Microsoft/TypeScript
GitHub ã§èŠã'this' implicitly has type 'any' in a function used within a conditional expression
Open
#54,723 opened on 2023幎6æ21æ¥
BugDomain: This-TypingHelp Wanted
Repository metrics
- Stars
- Â (48,455 stars)
- PR merge metrics
-  (å¹³åããŒãž 6d 17h) (30d ã§ 9 merged PRs)
説æ
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