Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Inconsistency between type literal and interface assignability to index signature #25880

Closed
ghost opened this issue Jul 23, 2018 · 3 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@ghost
Copy link

ghost commented Jul 23, 2018

TypeScript Version: 3.1.0-dev.20180721

Code

type T = { x: number };
interface I { x: number; }

let index: { [key: string]: number };
let t: T = { x: 0 };
let i: I = { x: 0 };

index = { x: 0 }; // OK
index = t; // OK
index = i; // Error

Expected behavior:

No error.

Actual behavior:

src/a.ts:10:1 - error TS2322: Type 'I' is not assignable to type '{ [key: string]: number; }'.
  Index signature is missing in type 'I'.

10 index = i; // Error
@mhegazy
Copy link
Contributor

mhegazy commented Jul 23, 2018

Duplicate of #15300

@mhegazy mhegazy marked this as a duplicate of #15300 Jul 23, 2018
@mhegazy mhegazy added the Duplicate An existing issue was already created label Jul 23, 2018
@ghost
Copy link
Author

ghost commented Jul 23, 2018

What's relevant to DefinitelyTyped/DefinitelyTyped#27465 is, how can one constrain a type such that all of the values of that type extend something?

type ValuesAreStringOrNumber = { [key: string]: string | number };
declare function f<T extends ValuesAreStringOrNumber>(t: T): T;
interface XNumber {
    x: number;
}
const o = f<XNumber>({ x: 0 });

The example works perfectly without the type annotation (and errors on { x: {} }), but fails when an interface type is provided.

@typescript-bot
Copy link
Collaborator

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants