microsoft / microsoft/TypeScript
【bug】ThisType is invalid when using param on generic object
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
ThisType Object 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
type ILifetime = {
onShareTimeline: () => string
}
function createObject<D>(obj: D & ILifetime & ThisType<D>) {
console.warn(obj)
}
// working
createObject({
title: 'demo',
onShareTimeline() {
return ''
}
})
// not working
createObject({
title: 'demo',
onShareTimeline() {
return this.title
}
})
🙁 Actual behavior
The generic type is broken
🙂 Expected behavior
The generic type should contain the object properties as those are finally included into the result
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 and the createObject generic example, comparing the working callback with the version that reads this.title. Investigate how ThisType is applied during generic object contextual typing; done means the inferred object type includes title and the callback can return this.title without an error.
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
- 25/100