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

index operator and object spread #17849

Closed
bradzacher opened this issue Aug 16, 2017 · 5 comments
Closed

index operator and object spread #17849

bradzacher opened this issue Aug 16, 2017 · 5 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@bradzacher
Copy link
Contributor

TypeScript Version: 2.4.2

Code

interface HasIndex {
    [key : string] : number
}
interface HasPropWithHasIndex {
    hasIndex ?: HasIndex
}

// one - works fine
const obj1 : HasPropWithHasIndex = {
    hasIndex: {
        a: 1
    }
}

// two - works fine
const test2 = { a: 1 }
const obj2 : HasPropWithHasIndex = {
    hasIndex: test2
}

// three - doesn't work
const obj = {
    a: 1,
    b: 2,
    c: 3,
}
const { a, ...test3 } = obj
// Error: Index signature is missing in type '{ b: number; c: number; }'.
const obj3 : HasPropWithHasIndex = {
    hasIndex: test3,
}

// four - test4 should be of type number, is instead of type any
const test4 = test3['a']

Expected behavior:
The obj3 assignment in test three should work without an issue.
test4 in test four should be of type number

Actual behavior:
The obj3 assignment in test three throws an error: Index signature is missing in type '{ b: number; c: number; }'.
test4 is of type any.

It looks like when you use the spread operator, typescript changes the type of the index operator to return any.

@DanielRosenwasser
Copy link
Member

@ahejlsberg, should this work with respect to changes made in #7029?

@olegdunkan
Copy link

test2 and {a:1} originates in an object literal, test3 originates from rest type, for test2 and {a:1} we can apply #7029 for test3 probably not, but there is not any info in spec for both cases!!!

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Aug 29, 2017
@RyanCavanaugh
Copy link
Member

This works as expected now

@benhamlin
Copy link

This is an old issue but was never actually fixed? You can paste the original code into the playground and all the same errors occur.

@koba04
Copy link

koba04 commented Aug 13, 2020

@RyanCavanaugh
I think this issue has not been fixed yet.
I've created an example of this. Is it expected behavior that var error: A = rest; is an error, but var ok2: A = {...rest}; is not?

type A = { [key: string]: string };

var obj = { foo: 'foo', bar: 'bar', baz: 'baz'};
const {...rest} = obj;

const mapped: { [a in keyof typeof rest]: string } = rest;

var ok: A = obj;
var ok2: A = {...rest};
var ok3: A = mapped
var error: A = rest;   // An error occurred

https://www.typescriptlang.org/play?#code/C4TwDgpgBAglC8UDeUDaBrCIBcUDOwATgJYB2A5gLq4EkVQC+A3AFAsBuAhoVAPYBGAKwTIoAM169cAcgm9pAGij9uMlYUXLOALzU7pzFgGNepAsgB0VwhAIMRAwa2OnzAW05hIAE1wpUnFBkUJggvGJQoJDhUDYE1PhEZOSMInHAzlw8vOi4cIiOrFl86ABMeSJIVhbphsU5AMwViB5eEN4c3FAQhIS8hM2xthksQA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

8 participants