Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for typing definition keys #529

Open
dwright20 opened this issue Aug 18, 2024 · 0 comments
Open

Support for typing definition keys #529

dwright20 opened this issue Aug 18, 2024 · 0 comments

Comments

@dwright20
Copy link

Staying in line with using type safety, it would be great if typing support was built into the library by default for the type definition keys. Take a look at the following:

type DefinitionType<T> = {
	[K in keyof T]: IField;
};

interface TypedIntermediateTypeOptions<T> extends IntermediateTypeOptions {
	readonly definition: DefinitionType<T>;
}
class ExtendedInterfaceType<T> extends InterfaceType {
	constructor(name: string, options: TypedIntermediateTypeOptions<T>) {
		super(name, options);
	}
}

export interface DataItem {
	day: number;
	month: number;
	source: DataProvider;
	timestamp: number;
	year: number;
}

// will give an error since source is missing
export const DataItemType = new ExtendedInterfaceType<DataItem>("DataItem", {
	definition: {
		timestamp: GraphqlType.int(),
		year: GraphqlType.int(),
		month: GraphqlType.int(),
		day: GraphqlType.int(),
	},
});

// will not give an error since all properties are implemented 
export const DataItemType = new ExtendedInterfaceType<DataItem>("DataItem", {
	definition: {
		timestamp: GraphqlType.int(),
		year: GraphqlType.int(),
		month: GraphqlType.int(),
		day: GraphqlType.int(),
                source: DataProviderEnumType.attribute(),
	},
});

We've made the choice to make the type required on the interface, ensuring consistent typing everywhere, but for this particular library it probably makes more sense to make it optional (will also ensure it isn't a breaking change).

I don't think the implementation would be a heavy lift, and am happy to submit a PR whenever I get a chance, but what do you all think about this functionality? Agree it makes sense to build it into the library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant