Skip to content

Support for typing definition keys #529

@dwright20

Description

@dwright20

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions