Skip to content

Iterating through object and assigning to its properties leads to 'never' #38735

Closed

Description

TypeScript Version: 3.9.2

Search Terms: Type is not assignable to type 'never'

Code

interface TraitsSummary {
  name: string;
  age: number;
  gender: 'male' | 'female' | 'other';
}

type TraitsDetailed = {
    [K in keyof TraitsSummary]: {
      value: TraitsSummary[K];
      common: boolean;
      required: boolean;
    }
}

const values: TraitsSummary = {
  name: 'Default',
  age: 0,
  gender: 'other'
};

const details: TraitsDetailed = {
  name: { value: 'Miranda', common: true, required: true },
  age: { value: 33, common: true, required: true },
  gender: { value: 'female', common: true, required: false }
};

const keys = Object.keys(details) as Array<keyof TraitsSummary>;

for (let i = 0; i < keys.length; i++) {
  values[keys[i]] = details[keys[i]].value;
}

Expected behavior:
TraitsSummary and TraitsDetailed have exactly the same keys.
values of props under the keys have matching types so there shouldn't be any error.

Actual behavior:

Type 'string | number' is not assignable to type 'never'.
  Type 'string' is not assignable to type 'never'.(2322)

Playground Link:
https://www.typescriptlang.org/play/?ssl=31&ssc=2&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgCpTsMBnAygVwFtC4oBPZAbwFgAoZZEOQiALmWzClAHMBuOgzg82jIgCNoA+shEgAJtHYByEgBsIy5AB9kymBHWadegPZgAFtGXSAvnTpgyABxTpMOACIQwmDfOQAXipBBmQAbQBpZFBkAGsIMlMYNAwsPCIScgBddhoZMOQANzg1fFF3dIJiUjIo7OlChgRTYlMQdnFTUw04EEamqAgAR3xgIflO7t7+0IZ7WgW6FpBOYtLy7HZKnGqsimD8hiYWFW94fDUwZQAaUOFRAAY7mTlFKBVzKyhlOltpZbtNaKXzANRbVIebDeUH+IIhGQnUSUdZlUTKACy4z68jgt2QLTaHWQXHKN2QQ1G4wgkxJUHKyFsLyEIjyqPK7AAzJzyYTCO12KSIOTKWMJoL6SgmaE3koqOz0QYjPi+QK6WSKSMxTT2PBwVK-gDaCs1gkyNh4QB5cQAKwgCDAADozdgABQgvzYACUyDgFoAglAMGQADxm5KQqqZWoAPiNMFMUGQro0YBi8MefHTIfiiWwjo0IB4lizwAA1GWfUcFdhwi7wsBstl4R6wbX643so6Smi7EA

P.S.
I have dozens of errors like Type 'some_type' is not assignable to type 'never'.
All of them happens when I'm trying to assign to object property selected by dynamic key.
I fixed some of issues with generics

(<T, K extends keyof T>(obj: T, keys: K[]) => {

                for (let index = 0; index < keys.length; index++) {

                    const key = keys[index] as keyof T;
                    filters[key] = null; // or other actions

                }

})(obj, originalFields);

But that's not working when you need to map values of one object to another regarding keys are same.

Activity

  1. jack-williams commented on May 23, 2020

    @jack-williams
    Collaborator

    This behaviour was introduced here #30769, and has lots of relevant discussion.

  2. DzmVasileusky commented on May 25, 2020

    @DzmVasileusky
    Author

    Jack Williams (@jack-williams) Thanks, I'm aware of that change but it didn't give me clear explanation on how to adjust my code to it.
    To prevent that code from failing I need a monstrous typescript constructions which decrease a code readability awfully. And sometimes it's just super hard to invent such a construction and I have to put any to save the day.

  3. typescript-bot commented on Jun 13, 2020

    @typescript-bot
    Contributor

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

  4. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions