microsoft / microsoft/TypeScript
'this' implicitly has type 'any' in a function used within a conditional expression
Open
Nobody has claimed this yet.
Bug
Domain: This-Typing
Help Wanted
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
🔎 Search Terms
this any implicit 2683 widen
🕗 Version & Regression Information
- This is the behavior in every version I tried
⏯ Playground Link
Playground link with relevant code
💻 Code
// @strict: true
interface State {
value: string;
matches(value: string): boolean;
}
declare function macthesState(state: { value: string }, value: string): boolean;
declare function isState(state: unknown): state is State;
// this doesn't work
function test(config: unknown, prevConfig: unknown) {
if (isState(config)) {
return {
...config,
matches: isState(prevConfig)
? prevConfig.matches
: function (value: string) {
return macthesState(this, value);
},
};
}
return config;
}
// this works
function test2(config: State) {
return {
...config,
matches: function (value: string) {
return macthesState(this, value);
},
};
}
🙁 Actual behavior
'this' implicitly has type 'any' because it does not have a type annotation.(2683)
🙂 Expected behavior
inferred this type
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 compare the conditional-expression example with the working function. Trace the compiler behavior responsible for contextual typing of the nested function and verify the result under strict mode; done means the inferred this type matches the expected State shape without an explicit annotation.
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
- 40/100