microsoft / microsoft/TypeScript
ThisType is invalid when using object spread on generic object
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
ThisType Object Spread Generic
🕗 Version & Regression Information
- I was unable to test this on prior versions because I was not using this feature previously
⏯ Playground Link
Playground link with relevant code
💻 Code
function createObject<D extends {}>(def: D & ThisType<D>) { /* some code */ }
const namableObject = { name: '' };
// ThisType will be wrongly typed when using object spread
const spreadObject = createObject({
...namableObject,
get thisName() {
return this.name; // <-- Property 'name' does not exists on type '{}'
},
});
// Correctly typed otherwise
const fullObject = createObject({
name: '',
get thisName() {
return this.name;
},
});
🙁 Actual behavior
The generic type is broken when the given object value contains a spread object.
🙂 Expected behavior
The generic type should contain the spreaded object properties as those are finally included into the result object.
Comment
This may be related to #26412 and other issues like this one but I didn't found a workarround to get a ThisType correctly typed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked TypeScript Playground reproduction and compare the spread and non-spread calls to createObject. Trace how generic inference handles the spread object and ThisType context; done means the getter in spreadObject recognizes the name property without regressing the existing fullObject case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100