microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
101.31k stars 12.53k forks source link

inconsistent behavior between types and interfaces on assignment to a key type #51690

Open DetachHead opened 2 years ago

DetachHead commented 2 years ago

Bug Report

πŸ”Ž Search Terms

interface key type assign

πŸ•— Version & Regression Information

5.0.0-dev.20221129

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Rec = {[K: string]: number}
interface Foo {a: number}
declare const foo: Foo
// Type 'Foo' is not assignable to type 'Rec'.
  //  Index signature for type 'string' is missing in type 'Foo'.
const foo2: Rec = foo 

type Bar = {a: number}
declare const bar: Bar
// no error
const bar2: Rec = bar

πŸ™ Actual behavior

error when assigning foo which uses the interface but not when assigning bar which uses the type

πŸ™‚ Expected behavior

either both of them should have an error or neither of them should

jcalz commented 2 years ago

Duplicate of #15300, which was recently closed, locked, and jettisoned into the void of space with some flavor of "working as intended".

fatcerberus commented 2 years ago

Someone should probably remove the In Discussion label from that issue…

DetachHead commented 2 years ago

i've run into issues like this many times. i think it should be added to the documentation somewhere that explains the difference between types and interfaces. perhaps here?