Skip to content

Following non-null assertion of optional field, it is still considered nullableΒ #55409

Closed
@orgads

Description

@orgads

πŸ”Ž Search Terms

"undefined is not assignable" assertion

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about common bugs

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.1.6#code/JYOwLgpgTgZghgYwgAgGIHt3IN4FgBQAkALYCeAbnFAFzIDOYUoA5gNwEC+BoksiKAISo4CJClQD8tBkxBtOBAjACuIBGGDoQyABZwQAEwA2EDOgAUAfRiZaZgJQ4u+JavWbtewyaFRzAIypaX0c8ImAYZHMAQkCoADoySih7UUIoCDBlKBB2Ii9jU0wAqns85yA

πŸ’» Code

interface Foo {
	myvar: string;
}
interface Bar {
	myvar?: string;
}

function handleFoo(_foo: Foo) {}

function handleBar(bar: Bar) {
	if (!bar.myvar)
		return;
	handleFoo(bar);
}

πŸ™ Actual behavior

Argument of type 'Bar' is not assignable to parameter of type 'Foo'.
  Types of property 'myvar' are incompatible.
    Type 'string | undefined' is not assignable to type 'string'.
      Type 'undefined' is not assignable to type 'string'.

πŸ™‚ Expected behavior

It should work, as I verified myvar is defined.

Additional information about the issue

No response

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