-
Notifications
You must be signed in to change notification settings - Fork 13k
Description
TypeScript Version: 4.1.0-beta
Search Terms:
pick, generic function, keyof
Code
interface IRecord {
id: number,
name: string
}
type FilterProperties<T> = Pick<
T,
{ [K in keyof T]: T[K] extends string ? K : never }[keyof T]
>;
function extract<T>(data: T[], field: keyof FilterProperties<T>): void {
const record: T = data[0];
const value: string = record[field]; // see error below
}
const data: IRecord[] = [{ id: 1, name: "one" }, { id: 2, name: "two" }]
extract<IRecord>(data, "id"); // expected: error that id can't be used as a parameter since it's not a string
extract<IRecord>(data, "name") // no error, expected behaviour
Expected behavior:
I'm trying to create a function that receives an object/an array of objects of type T
and a field name but the user should be able to call the function only if the type of that field in T
is string
. FilterProperties
is just a type (union) that picks those properties from T
and can be easily extended to filter any kind of properties.
Since I was able to call the function correctly on the last line (because name
is a field of type string
, I was expecting to be able to extract the value in the function too, but I get the error below. Everything works if you define everything explicitly (no generics).
Actual behavior:
Type 'T[{ [K in keyof T]: T[K] extends string ? K : never; }[keyof T]]' is not assignable to type 'string'.
Type 'T[T[keyof T] extends string ? keyof T : never]' is not assignable to type 'string'.
Type 'T[keyof T]' is not assignable to type 'string'.
Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'string'.
Type 'T[string]' is not assignable to type 'string'.ts(2322)
Let me know if I should update the title to be more descriptive.