Skip to content

Object spread should not exclude setter-only properties #26337

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
ajafff opened this issue Aug 9, 2018 · 2 comments
Closed

Object spread should not exclude setter-only properties #26337

ajafff opened this issue Aug 9, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@ajafff
Copy link
Contributor

ajafff commented Aug 9, 2018

TypeScript Version: 3.1.0-dev.20180808

Search Terms:

Code

const obj = {
    foo: 1,
    ...{set bar(v: number) {}}
};

Expected behavior:

obj is inferred as {foo: number, bar: undefined}.
Spreading a setter-only property uses undefined as the property's value.
Likewise the following evaluates to true:

({
    foo: 1,
    ...{set foo(v) {}}
}).foo === undefined;

Actual behavior:

obj is inferred as {foo: number}.

Playground Link: https://agentcooper.github.io/typescript-play/#code/MYewdgzgLgBCBGArGBeGBvAUDHMBmIIAXDAIwA02uAdLehAKazwCGATgBQBuJYArgFt4DNgEoMAXwmYJAbkxA

Related Issues:
#11596
#21759
#17793

@ghost ghost added the Bug A bug in TypeScript label Aug 9, 2018
@ajafff
Copy link
Contributor Author

ajafff commented Aug 9, 2018

This might be unnecessary if #26338 lands. The special handling of setter-only properties could then be removed completely

@sandersn
Copy link
Member

From the type-level perspective:

A set-only property on the rhs introduces a property of type undefined that does not union with the same property's type from the lhs.

sandersn added a commit that referenced this issue Oct 29, 2018
Previously they were skipped. The runtime behaviour is to create a
property of type undefined, unlike (for example) spreading numbers or
other primitives. So now spreading a set-only accessor creates a
property of type undefined:

```ts
const o: { foo: undefined } = { ...{ set foo(v: number) { } } }
```

Notably, `o.foo: undefined` not `number`.

Fixes #26337
@sandersn sandersn added the Fixed A PR has been merged for this issue label Oct 29, 2018
sandersn added a commit that referenced this issue Oct 29, 2018
* Set-only accessors spread to undefined

Previously they were skipped. The runtime behaviour is to create a
property of type undefined, unlike (for example) spreading numbers or
other primitives. So now spreading a set-only accessor creates a
property of type undefined:

```ts
const o: { foo: undefined } = { ...{ set foo(v: number) { } } }
```

Notably, `o.foo: undefined` not `number`.

Fixes #26337

* Fix isSpreadableProperty oversimplification
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants