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

Possibility of undefined from optional properties is not honoured in spread #35983

Closed
OliverJAsh opened this issue Jan 3, 2020 · 2 comments
Closed
Labels
Unactionable There isn't something we can do with this issue

Comments

@OliverJAsh
Copy link
Contributor

OliverJAsh commented Jan 3, 2020

TypeScript Version: 3.6.3

Search Terms: spread optional undefined object properties property

Code

Here's an example of spreading an object with optional properties:

const myQueryDefaults: { a: number; } = { a: 1 };
const myQuery: { a?: number; } = {};

const result = { ...myQueryDefaults, ...myQuery };

// type is `number`
// runtime value is `number`
result.a;

The above example makes sense. However, an optional property does not only mean "this property may not exist" ({})—it also means "this property may exist with a value of undefined" ({ a: undefined }). (Related: #13195.)

As a result of this, the type information may mismatch the actual types at runtime:

const myQueryDefaults: { a: number; } = { a: 1 };
const myQuery: { a?: number;  } = { a: undefined };

const result = { ...myQueryDefaults, ...myQuery };

// type is `number`, but runtime value is `undefined`!!
// expected type: `number | undefined`
result.a;

… which could result in runtime exceptions:

result.a.toString(); // runtime exception, because `result.a` is `undefined`

Ideally optional properties would only mean "this property may not exist" (#13195), in which case the existing behaviour with spreading makes sense. However, in the interim, perhaps the behaviour of spreading should be changed?

Extracted from #13195 (comment)

Also potentially related: #31025

@OliverJAsh OliverJAsh changed the title undefined Optional properties are not honoured in spread Possibility of undefined from optional properties is not honoured in spread Jan 3, 2020
@MartinJohns
Copy link
Contributor

However, in the interim, perhaps the behaviour of spreading should be changed?

That would most likely break every codebase out there.

@RyanCavanaugh RyanCavanaugh added the Unactionable There isn't something we can do with this issue label Jan 6, 2020
@RyanCavanaugh
Copy link
Member

#13195 is the actionable version of this. This would be an unbelievably large and unergonomic breaking change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Unactionable There isn't something we can do with this issue
Projects
None yet
Development

No branches or pull requests

3 participants