Closed
Description
Section A causes no TS error in the source code below.
Section B causes a TS error in the source code below.
Both sections do the same thing and section B shouldn't yield any errors. (?)
The message "Type 'boolean' is not assignable to type 'string'" also makes no sense because prop
is clearly a boolean
now. (?)
(?) means: I am pretty sure, but not entirely sure, as I am new to Typescript. ^^
type Template = {
prop: number;
}
type Keys<Type> = keyof Type | (string & Record<never, never>);
type SomeType<T> = {
[Property in Keys<T>]?: Property extends keyof T ? boolean : string;
};
// section A: no TS error
let something: SomeType<Template> = {};
something.prop = true;
something.anotherProp = 'value';
// section B: TS error
// Type '{ prop: true; anotherProp: string; }' is not assignable to type 'SomeType<Template>'.
// Property 'prop' is incompatible with index signature.
// Type 'boolean' is not assignable to type 'string'
something = {
prop: true,
anotherProp: 'value',
};