Closed as not planned
Description
π Search Terms
"generic type inference"
π Version & Regression Information
TS v5.3.3
It's not a regression.
β― Playground Link
π» Code
type StringProps<Entity> = {
[K in keyof Entity as Entity[K] extends string ? K : never]: Entity[K];
};
type StringPropKey<Entity> = keyof StringProps<Entity>;
function logKey<E>(key: StringPropKey<E>) {
console.log(key);
}
function error<E extends { uuid: string }>() {
logKey<E>('uuid');
}
π Actual behavior
Compilation error: Argument of type 'string' is not assignable to parameter of type 'keyof StringProps<E>'. Type '"uuid"' is not assignable to type '(E[K] extends string ? K : never) | (E["uuid"] extends string ? "uuid" : never)'
π Expected behavior
There should not be a compilation error. As a workaround, we can explicitly check if E extends { uuid: string }
:
function error<E extends { uuid: string }>() {
// this
logKey<E extends { uuid: string } ? E : never>('uuid');
}
but this should not be required because E
already extends { uuid: string }
.
Additional information about the issue
No response