Initially, I created a type definition map where all properties are converted into numbers using keyof
.
type Numeric<T> = {
[K in keyof T]: number
}
Next, here is the structure of a class that will be used:
class Entity {
aNumber: number;
}
Following is a function that accepts a generic type argument and a local variable with the type Numeric<T>
. However, when trying to assign { aNumber: 1 }
, a compile error occurs.
const fn = <T extends Entity>() => {
const n: Numeric<T> = {
// ^
// Type '{ aNumber: number; }' is not
// assignable to type 'Numeric<T>'
aNumber: 1
};
};
The confusion arises from the fact that { aNumber: number; }
cannot be assigned to Numeric<T>
even though the type argument T
must extend from Entity
and have a key named
aNumber</code. This implies that <code>aNumber
should be the key of type T and therefore should be assignable to Numeric<T>
.