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

Wrong: All members must conform to the string index signature? #618

Open
Schokokex opened this issue Dec 24, 2020 · 1 comment
Open

Wrong: All members must conform to the string index signature? #618

Schokokex opened this issue Dec 24, 2020 · 1 comment

Comments

@Schokokex
Copy link

origin

i copied the Foo and Bar into ts playground and it works.

interface Foo {x:number; y:string}
const index: 'x'|'y' = fromSomewhere;
const foo1: Foo = { x:1, y:"a b c"}
const bar: string | number = foo1[index];
@daniellf99
Copy link

daniellf99 commented Feb 24, 2021

I also tried the examples and they did work for me too.

I think what the author actually meant is that when such an interface is defined:

interface Foo {
  [index: string]: number 
};

any members which have a string type key must have a number type value. In this case, trying to create an object member with a string type value would throw an error:

const foo: Foo = {
  x: 1, // Okay
  y: 'abc' // Error
};

If this is the case, this tutorial section examples are misleading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants