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

Different behaviours between undefined property and optional #9543

Closed
plantain-00 opened this Issue Jul 7, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@plantain-00
Copy link
Contributor

plantain-00 commented Jul 7, 2016

TypeScript Version: nightly (2.0.0-dev.20160706)

Code

type A = { a: number | undefined };
const b: A = { a: undefined };
const c: A = {}; // error, property a is missing in type {}

Expected behavior:
no error
Actual behavior:
error

Seems bug, or by design?

update: same when not in strict null check mode.

@plantain-00 plantain-00 changed the title Different behaviours between undefined property and optional, in strict null check mode Different behaviours between undefined property and optional Jul 7, 2016

@ahejlsberg

This comment has been minimized.

Copy link
Member

ahejlsberg commented Jul 7, 2016

This is by design. A property can be omitted only if it was declared with a ? modifier. So, { a?: number } says that a is optional and may be omitted (and undefined is automatically included in the type if it isn't explicitly specified), whereas { a: number | undefined } says that a must be specified and undefined is one of the permitted values.

@plantain-00 plantain-00 closed this Jul 7, 2016

@Microsoft Microsoft locked and limited conversation to collaborators Jun 19, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.