I am working with GraphQL and I want to have strict typing in gql
. Is it possible to make result
variable to be shape of ResultData
i.e. with the latest version of TypeScript. It is only about typing, not about runtime.
interface Data {
one: string;
two: number;
three: {
four: string;
five: {
six: Date;
};
};
}
// true means that this field must be added to query
type RecursivePartial<T> = {[P in keyof T]?: RecursivePartial<T[P]> | true};
function gql<T>(fields: RecursivePartial<T>) {
// Some code about generating GraphQL query
}
const result = gql<Data>({one: true, three: {five: {six: true}}});
// type ResultData {
// one: string;
// three: {
// five: {
// six: Date;
// };
// };
// }