How to get Typescript Type from ObjectRef and InputObjectRef? #735
-
Can I get vanilla Typescript Type from ObjectRef and InputObjectRef?
I want to get
How to do this? Thanks for your help! |
Beta Was this translation helpful? Give feedback.
Answered by
hayes
Jan 4, 2023
Replies: 1 comment 2 replies
-
There should probably be a docs section on this somewhere, but here are a few examples of helpers you could use: import SchemaBuilder, {
InputShapeFromTypeParam,
InputTypeParam,
InputTypeRef,
ObjectRef,
SchemaTypes,
ShapeFromTypeParam,
TypeParam,
} from '@pothos/core';
type InferInputType<T extends InputTypeRef<unknown>> = T extends InputTypeRef<infer S> ? S : never;
type InferOutputType<T extends ObjectRef<unknown>> = T extends ObjectRef<infer S> ? S : never;
const input: InferInputType<typeof ExampleInput> = { id: 1, ids: [] };
const output: InferOutputType<typeof User> = { firstName: 'a', lastName: 'b' };
// Support for lists, scalars, and other methods of referencing types
type BetterInferInputType<
Builder extends PothosSchemaTypes.SchemaBuilder<any>,
T extends InputTypeParam<Types>,
Types extends SchemaTypes = Builder extends PothosSchemaTypes.SchemaBuilder<infer S> ? S : never,
> = InputShapeFromTypeParam<Types, T, true>;
type BetterInferOutputType<
Builder extends PothosSchemaTypes.SchemaBuilder<any>,
T extends TypeParam<Types>,
Types extends SchemaTypes = Builder extends PothosSchemaTypes.SchemaBuilder<infer S> ? S : never,
> = ShapeFromTypeParam<Types, T, false>;
const input2: BetterInferInputType<typeof builder, ['String']> = ['a', 'b'];
const output2: BetterInferOutputType<typeof builder, ['String']> = ['a', 'b']; |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
trancong12102
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There should probably be a docs section on this somewhere, but here are a few examples of helpers you could use: