I have developed a utility type named DataType
that takes in a parameter T restricted to the type keyof MyObject
. When the key exists in MyObject
, DataType
will return the property type from MyObject
; otherwise, it will simply return T.
interface MyObject {
foo: string;
bar: boolean;
}
export type DataType<T extends keyof MyObject> = T extends keyof MyObject
? MyObject[T]
: T;
If I now input any key from MyObject
, it will provide the corresponding data type:
type StrType=DataType<'foo'>; // data type is string
However, when I supply a data type other than a string to this helper function, I encounter an error message saying
Type 'type' does not satisfy the constraint '"foo"'
:
type numType=DataType<number>;
Is there a way to remove the type constraint while still retaining IntelliSense support for the keys of MyObject
?