question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Index signature is assignable to weak type whose properties don't match the signature type

See original GitHub issue

From 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:open
  • Created 5 years ago
  • Reactions:4
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mattmccutchencommented, Sep 17, 2018

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:

interface Foo {
    a: number;
    b?: string;
}
interface Bar {
    a: number
    [n: string]: number;
}
let b: Bar = {a: 42, b: 43};
let f: Foo = b;  // OK; expect error?
1reaction
sandersncommented, Sep 17, 2018

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found