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

Union type to copy properties to other object not assignable to never #32597

Closed
greg-hornby-roam opened this issue Jul 28, 2019 · 4 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@greg-hornby-roam
Copy link

TypeScript Version: 3.5.x

Search Terms: union properties object never

Code

interface IUser {
    id: number;
    createdAt: Date;
    name: string;
    age: number;
    isActive: boolean;
}

//Imagine this array is an array of strings matching the property names of IUser
let properties = [] as any[] as (keyof IUser)[];

let user: IUser = {} as any; //Imagine this being fetched from a database
let constructedUser: IUser = {} as any;
for (let prop of properties) {
    constructedUser[prop] = user[prop];
}

Expected behavior: No errors

Actual behavior:

Type 'string | number | boolean | Date' is not assignable to type 'never'.
  Type 'string' is not assignable to type 'never'.

This used to work on 3.4, seems broken on 3.5

Playground Link:
3.5: (Broken)
https://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAgJIFUDO1kG8BQyRywAJgFzIgCuAtgEbQDchxCUEckpAgmJQBEuEFsSpxaESpjBRQAc1HE48qVTqMoSosEw8EYYADc19APZmANpxAsAvvnwB6J6lorQKMAAtdyOFBQcACeJJj+IP6BIchmMMgyciDy4e5gCL7JyD4oAA5QZrnQYKEgEhDhcWhY0PjWYMj5hcXAFcgAvMgA2gC6-uFwIMG9-cgAFADWEMFVGNhQAJS9LHUQDdTzlHM4nbh2o4PBTMgubh4gXr7hjArIMGsZEKR3BbT+yKRccPRw2KsNCDMIES1AMTxqUC2EI6eH2vwiR3wMDMUHG9UaBVysXiTSKUEMFQWeFYREBwNkoO4EK6uL6nQ20BpmJ69iAA

3.3: (Working)
https://www.typescriptlang.org/play/?ts=3.3.3#code/JYOwLgpgTgZghgYwgAgJIFUDO1kG8BQyRywAJgFzIgCuAtgEbQDchxCUEckpAgmJQBEuEFsSpxaESpjBRQAc1HE48qVTqMoSosEw8EYYADc19APZmANpxAsAvvnwB6J6lorQKMAAtdyOFBQcACeJJj+IP6BIchmMMgyciDy4e5gCL7JyD4oAA5QZrnQYKEgEhDhcWhY0PjWYMj5hcXAFcgAvMgA2gC6-uFwIMG9-cgAFADWEMFVGNhQAJS9LHUQDdTzlHM4nbh2o4PBTMgubh4gXr7hjArIMGsZEKR3BbT+yKRccPRw2KsNCDMIES1AMTxqUC2EI6eH2vwiR3wMDMUHG9UaBVysXiTSKUEMFQWeFYREBwNkoO4EK6uL6nQ20BpmJ69iAA

Related Issues:

@greg-hornby-roam greg-hornby-roam changed the title String union type to copy properties to other object not assignable to never Union type to copy properties to other object not assignable to never Jul 28, 2019
@fatcerberus
Copy link

This is a consequence of #30769.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 29, 2019
@greg-hornby-roam
Copy link
Author

I guess it's a design limitation that if the target and source objects are the same interface, and are being indexed with the same key it can't infer that in that one case the types should match?

@fatcerberus
Copy link

fatcerberus commented Jul 30, 2019

The issue is that the compiler only sees the types when checking this - it doesn’t know that prop evaluates to the same key on both sides, only that it’s type keyof IUser. That isn’t enough information to guarantee the assignment is safe, so it rejects it.

See also: #31445

@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