-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Compile Error when using generic type with [K in keyof T]?: XXX #31070
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Mapped TypesThe issue relates to mapped typesThe issue relates to mapped types
Milestone
Description
TypeScript Version: 3.4.4
Search Terms:
keyof index-properties generic-type
Code
type Numeric<T> = {
[K in keyof T]?: number
}
class Entity {
aNumber: number;
}
const fn = <T extends Entity>() => {
const n: Numeric<T> = {
// . ^
// . Type '{ aNumber: number; }' is not assignable to type 'Numeric<T>'
aNumber: 1
};
};Expected behavior:
Should not report compile error since in the type constraint of function fn was <T extends Entity> which means T should have aNumber: number and { aNumber: 1 } should be satisfied.
Actual behavior:
Compile error said Type '{ aNumber: number; }' is not assignable to type 'Numeric<T>'.
Related Issues:
https://stackoverflow.com/questions/55804034/keyof-reported-compile-error-when-using-generic-type
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: Mapped TypesThe issue relates to mapped typesThe issue relates to mapped types