Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Why index signature type should be assignable to all known members? #29924

Closed
mohsen1 opened this issue Feb 15, 2019 · 1 comment
Closed

Why index signature type should be assignable to all known members? #29924

mohsen1 opened this issue Feb 15, 2019 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@mohsen1
Copy link
Contributor

mohsen1 commented Feb 15, 2019

I want to define an object type where I explicitly define types of known members and then have a different type for unknown members (type of the index signature):

interface Known {
    foo?: () => boolean;
}

interface Unknown {
    [k: string]: () => number;
}

type All = Known & Unknown;

let a: All = {
    foo: () => true
}

let b: All = {
    randomKey: () => 1
}

With above code, TypeScript complains that "Property foo is incompatible with index signature".

**Why type of property foo has to be compatible with index signature?

This issue originally came up here: typescript-eslint/typescript-eslint#272 (comment)

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 15, 2019
@RyanCavanaugh
Copy link
Member

Why type of property foo has to be compatible with index signature?

That's what an index signature means - it applies to all properties, not just those not declared. See #17867 and related issues for more discussion.

@mohsen1 mohsen1 closed this as completed Feb 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

2 participants