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

keyof an interface that extends an anyObject #35600

Closed
dolphinlin opened this issue Dec 10, 2019 · 5 comments
Closed

keyof an interface that extends an anyObject #35600

dolphinlin opened this issue Dec 10, 2019 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@dolphinlin
Copy link

dolphinlin commented Dec 10, 2019

TypeScript Version: 3.7.2

Search Terms:

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
type AnyObject = {
  [key: string]: any;
};

interface Test extends AnyObject {
  name: string;
  age: number;
  handsome: boolean;
}

type TestKey = keyof Test;

const testObject: Test = {
  name: 'Dolphin',
  age: 999,
  handsome: true,
};

Expected behavior:

type TestKey = 'name' | 'age' | 'handsome';

Actual behavior:

type TestKey = string | number;

Playground Link:
https://www.typescriptlang.org/play/index.html#code/C4TwDgpgBAggdiA8gIwFYQMbCgXigbwCgooBtAawhAC4oBnYAJwEs4BzAXVoEMEBuQgF8BhVsAiMAZtwzQAKhAZQIAD3FwAJnVgIU6LAWJQ43ALYRaDFuwElubC8YCup5BIGDChUJCgKGANJUuFCUIAD2kn6KwCIY4XBK4gx6mMC0-th4RCQm5rQA5AAi4QA2YAAWrAUANEb2jgCczUJ8QA

Related Issues:

@dolphinlin
Copy link
Author

dolphinlin commented Dec 10, 2019

if Test ain't extends AnyObject, TestKey will be correct.

@jcalz
Copy link
Contributor

jcalz commented Dec 10, 2019

This is working as intended. testObject.blarf = 1; is acceptable because Test has an index signature because Test extends AnyObject. so keyof Test is going to reflect that index signature type which absorbs all string literals.

@dolphinlin
Copy link
Author

This is working as intended. testObject.blarf = 1; is acceptable because Test has an index signature because Test extends AnyObject. so keyof Test is going to reflect that index signature type which absorbs all string literals.

Yes, agree ur opinion, but that maybe shouldn’t lost the keys of Test type ('name' | 'age' | 'handsome').

@jcalz
Copy link
Contributor

jcalz commented Dec 10, 2019

So is this a duplicate of #33471 then?

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Dec 10, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants