Index signature is assignable to weak type whose properties don't match the signature type
See original GitHub issueFrom https://stackoverflow.com/q/52368008 . Based on https://github.com/Microsoft/TypeScript/pull/16047#issue-122101801, it appears that @sandersn may have thought about the rule, but it still makes no sense to me and no rationale is stated.
TypeScript Version: master (394ee31a56c40033f31a3a8461ec267463033194)
Search Terms: weak type index signature
Code
interface Foo {
a?: string;
}
interface Bar {
[n: string]: number;
}
declare let b: Bar;
// No error; expected "Type 'Bar' has no properties in common with type 'Foo'."
let a: Foo = b;
Expected behavior: Error: “Type ‘Bar’ has no properties in common with type ‘Foo’.”
Actual behavior: No error.
Playground Link: link
Related Issues: Possibly #9900
Issue Analytics
- State:
- Created 5 years ago
- Reactions:4
- Comments:8 (8 by maintainers)
Top Results From Across the Web
How do I prevent the error "Index signature of object type ...
I've tried explicitly casting the type by: let secondValue: string = <string>someObject[key]; Or is my scenario just not possible with --noImplicitAny ?
Read more >Idea: 'rest' index signatures and the 'error' type · Issue #7765
Property 'delayed' is incompatible with index signature. Type '{ hello: string; }' is not assignable to type 'string'.
Read more >Overview - TypeScript
If X contains a numeric index signature, keyof X is a union of number and the literal types representing string-like and symbol-like properties,...
Read more >Index Signatures in TypeScript - Dmitri Pavlutin
Property 'baseSalary' is incompatible with index signature. Type 'string' is not assignable to type 'number'.
Read more >Documentation - TypeScript 4.4
Property 'toUpperCase' does not exist on type 'unknown'. ... For example, we can write a type with an index signature that takes string...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I didn’t know a “Bug” could be “In Discussion”. 😄
For that matter, if we interpret an index signature as “there might exist a property of any name”, why do we ever allow a type with an index signature to be assignable to a type with an incompatible optional property, regardless of whether the target type is weak? Are there real-world cases in which the error would be undesirable? Example:
Ah, I didn’t see the types. 😃 I have the weak type implementation so much in my head that I just looked at properties.
So it sounds like the correct check for index signatures is to make sure that the signature type is assignable to the union of the weak type’s property types.