TypeScript Version: 3.6.0-dev.20190704 (3.4.3 doesn't have this bug)
Search Terms:
index signature, unknown, assignable, interface
Code
interface A {
properties?: { [Key: string]: unknown };
}
type Bar = {
key?: "value";
};
interface Baz {
key?: "value";
}
interface B extends A { // ok
properties?: Bar;
}
interface C extends A { // error: Baz is not assignable to { [Key: string]: unknown } -> Index signature is missing
properties?: Baz;
}
Expected behavior:
No error expected, interface C extends A correctly.
Actual behavior:
Error because interface C does not extend interface A
Playground Link:
http://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgILIN4ChnIA5QD2e0YwEAzgPwBcmyA2gNIQCedFYUoA5gLp0AriADWIQgHcQyAL4BuLDKxYwrEsgBCcKMgC8mHMhFtayAEQA3OABtBEMwvnLQkWIhRaAXgdzHWpyxs7B0VncGh4JE1kCAAPSBAAEwo0egB6NORCEUMCYlJyajotKEcw10iUAGEY+IgklPQMZAyYqCIoYrhvYBTxMGQ4CgpgHhA4ACNrFDBCemY2Di5eAWRhMUlpGWQAWgA+ZABJJLjkEbG4MEEoFF7kAFtekZAeXKISKDJKUy8yoA
Related Issues:
enums)I believe this is working as intended: object literal types have implicit index signatures, while interfaces do not. See the implementation here: #7029
Thanks @jack-williams , I didn't know about this specific assignability rule. Do you know if there is a comprehensive list of type assignability rules out there? I tried to search for one but couldn't find.
Not really - If you want to know the absolute truth you need to go here:
The spec is pretty good, although incomplete:
And something to keep an eye on:
I hate that checker.ts is so absolutely ginormous that GitHub refuses to display it - it makes it impossible to link to specific lines.
The reason intellisense for TypeScript is so performant is because the TypeScript team want to have auto-completion while editing checker.ts[1] - so if anything, we should be encouraging them to make it larger.
[1] I made that up
I need to add this to the interfaces vs type aliases section.
... if anything, we should be encouraging them to make it larger.
I feel seen. Also we should make the binder incremental.
Most helpful comment
The reason intellisense for TypeScript is so performant is because the TypeScript team want to have auto-completion while editing
checker.ts[1] - so if anything, we should be encouraging them to make it larger.[1] I made that up