Skip to content

Unexpected "property is not assignable to type 'undefined'" on dynamic assignmentΒ #61409

Closed as duplicate of#32693
@DimaIT

Description

@DimaIT

πŸ”Ž Search Terms

strictNullChecks "not assignable" "to type 'undefined'"

πŸ•— Version & Regression Information

  • This changed between versions 3.3.3 and 3.5.1
    All newer versions have this behavior

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgHIHsAmEAKV0AOAzsgN4BQyVycA-AFzIgCuAtgEbQDcl17DyImCigA5jwC+5cgnQghydCNGg4AG0YZseQiQC8ZCT1nywyMHCiiIYTVlz5iyA6SPS1N5AUcBGRgGsIAE90GDR7HScDAHI4aJ4LKxsAbW9CHwBdZ0VlVTVU3wyuZAB6EuQAdyV-IndPNIIAJgDg0PDtR31kWOiaEkCQsK0HXQTLazACwkasgyVgFRB1Kaai0vKAUSh8KGkTBQaiRgBBbbgggB4BtuHIogA+bOSi8hglZAAKfbMGxTDDgCUZF4VESExWsxyCzyEOKZWQWx25CkQA

πŸ’» Code

interface NodeProps {
    a?: number;
    b?: string;
}

const original: NodeProps = {};
const target: NodeProps = {};

let prop1: keyof NodeProps = 'a';
target[prop1] = original[prop1]; // works

let prop2: keyof NodeProps = 'a' as keyof NodeProps;
target[prop2] = original[prop2]; // Error

const props: Array<keyof NodeProps> = [];
for (const prop of props) {
    target[prop] = original[prop]; // Error
}

πŸ™ Actual behavior

Got unexpected errors:

Type 'string | number | undefined' is not assignable to type 'undefined'.
  Type 'string' is not assignable to type 'undefined'.

πŸ™‚ Expected behavior

Should produce no errors

Additional information about the issue

Seems to be closely related to #59969 - disabling strictNullChecks option changes the error text, but essentially error stays the same.

Error produced with strictNullChecks:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions