-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve detection of containing object literals to improve contextual…
… `this` typing
- Loading branch information
Showing
4 changed files
with
305 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//// [tests/cases/conformance/expressions/thisKeyword/thisInObjectLiterals2.ts] //// | ||
|
||
=== thisInObjectLiterals2.ts === | ||
// https://github.com/microsoft/TypeScript/issues/54723 | ||
|
||
interface State { | ||
>State : Symbol(State, Decl(thisInObjectLiterals2.ts, 0, 0)) | ||
|
||
value: string; | ||
>value : Symbol(State.value, Decl(thisInObjectLiterals2.ts, 2, 17)) | ||
|
||
matches(value: string): boolean; | ||
>matches : Symbol(State.matches, Decl(thisInObjectLiterals2.ts, 3, 16)) | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 4, 10)) | ||
} | ||
|
||
declare function macthesState(state: { value: string }, value: string): boolean; | ||
>macthesState : Symbol(macthesState, Decl(thisInObjectLiterals2.ts, 5, 1)) | ||
>state : Symbol(state, Decl(thisInObjectLiterals2.ts, 7, 30)) | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 7, 38)) | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 7, 55)) | ||
|
||
declare function isState(state: unknown): state is State; | ||
>isState : Symbol(isState, Decl(thisInObjectLiterals2.ts, 7, 80)) | ||
>state : Symbol(state, Decl(thisInObjectLiterals2.ts, 8, 25)) | ||
>state : Symbol(state, Decl(thisInObjectLiterals2.ts, 8, 25)) | ||
>State : Symbol(State, Decl(thisInObjectLiterals2.ts, 0, 0)) | ||
|
||
function test(config: unknown, prevConfig: unknown) { | ||
>test : Symbol(test, Decl(thisInObjectLiterals2.ts, 8, 57)) | ||
>config : Symbol(config, Decl(thisInObjectLiterals2.ts, 10, 14)) | ||
>prevConfig : Symbol(prevConfig, Decl(thisInObjectLiterals2.ts, 10, 30)) | ||
|
||
if (isState(config)) { | ||
>isState : Symbol(isState, Decl(thisInObjectLiterals2.ts, 7, 80)) | ||
>config : Symbol(config, Decl(thisInObjectLiterals2.ts, 10, 14)) | ||
|
||
return { | ||
...config, | ||
>config : Symbol(config, Decl(thisInObjectLiterals2.ts, 10, 14)) | ||
|
||
matches: isState(prevConfig) | ||
>matches : Symbol(matches, Decl(thisInObjectLiterals2.ts, 13, 16)) | ||
>isState : Symbol(isState, Decl(thisInObjectLiterals2.ts, 7, 80)) | ||
>prevConfig : Symbol(prevConfig, Decl(thisInObjectLiterals2.ts, 10, 30)) | ||
|
||
? prevConfig.matches | ||
>prevConfig.matches : Symbol(State.matches, Decl(thisInObjectLiterals2.ts, 3, 16)) | ||
>prevConfig : Symbol(prevConfig, Decl(thisInObjectLiterals2.ts, 10, 30)) | ||
>matches : Symbol(State.matches, Decl(thisInObjectLiterals2.ts, 3, 16)) | ||
|
||
: function (value: string) { | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 16, 20)) | ||
|
||
return macthesState(this, value); | ||
>macthesState : Symbol(macthesState, Decl(thisInObjectLiterals2.ts, 5, 1)) | ||
>this : Symbol(__object, Decl(thisInObjectLiterals2.ts, 12, 10)) | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 16, 20)) | ||
|
||
}, | ||
}; | ||
} | ||
|
||
return config; | ||
>config : Symbol(config, Decl(thisInObjectLiterals2.ts, 10, 14)) | ||
} | ||
|
||
function test2(config: State) { | ||
>test2 : Symbol(test2, Decl(thisInObjectLiterals2.ts, 23, 1)) | ||
>config : Symbol(config, Decl(thisInObjectLiterals2.ts, 25, 15)) | ||
>State : Symbol(State, Decl(thisInObjectLiterals2.ts, 0, 0)) | ||
|
||
return { | ||
...config, | ||
>config : Symbol(config, Decl(thisInObjectLiterals2.ts, 25, 15)) | ||
|
||
matches: function (value: string) { | ||
>matches : Symbol(matches, Decl(thisInObjectLiterals2.ts, 27, 14)) | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 28, 23)) | ||
|
||
return macthesState(this, value); | ||
>macthesState : Symbol(macthesState, Decl(thisInObjectLiterals2.ts, 5, 1)) | ||
>this : Symbol(__object, Decl(thisInObjectLiterals2.ts, 26, 8)) | ||
>value : Symbol(value, Decl(thisInObjectLiterals2.ts, 28, 23)) | ||
|
||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
//// [tests/cases/conformance/expressions/thisKeyword/thisInObjectLiterals2.ts] //// | ||
|
||
=== thisInObjectLiterals2.ts === | ||
// https://github.com/microsoft/TypeScript/issues/54723 | ||
|
||
interface State { | ||
value: string; | ||
>value : string | ||
> : ^^^^^^ | ||
|
||
matches(value: string): boolean; | ||
>matches : (value: string) => boolean | ||
> : ^ ^^ ^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
} | ||
|
||
declare function macthesState(state: { value: string }, value: string): boolean; | ||
>macthesState : (state: { value: string; }, value: string) => boolean | ||
> : ^ ^^ ^^ ^^ ^^^^^ | ||
>state : { value: string; } | ||
> : ^^^^^^^^^ ^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
|
||
declare function isState(state: unknown): state is State; | ||
>isState : (state: unknown) => state is State | ||
> : ^ ^^ ^^^^^ | ||
>state : unknown | ||
> : ^^^^^^^ | ||
|
||
function test(config: unknown, prevConfig: unknown) { | ||
>test : (config: unknown, prevConfig: unknown) => unknown | ||
> : ^ ^^ ^^ ^^ ^^^^^^^^^^^^ | ||
>config : unknown | ||
> : ^^^^^^^ | ||
>prevConfig : unknown | ||
> : ^^^^^^^ | ||
|
||
if (isState(config)) { | ||
>isState(config) : boolean | ||
> : ^^^^^^^ | ||
>isState : (state: unknown) => state is State | ||
> : ^ ^^ ^^^^^ | ||
>config : unknown | ||
> : ^^^^^^^ | ||
|
||
return { | ||
>{ ...config, matches: isState(prevConfig) ? prevConfig.matches : function (value: string) { return macthesState(this, value); }, } : { matches: (value: string) => boolean; value: string; } | ||
> : ^^^^^^^^^^^^ ^^ ^^^^^ ^^^^^^^^^ ^^^ | ||
|
||
...config, | ||
>config : State | ||
> : ^^^^^ | ||
|
||
matches: isState(prevConfig) | ||
>matches : (value: string) => boolean | ||
> : ^ ^^ ^^^^^ | ||
>isState(prevConfig) ? prevConfig.matches : function (value: string) { return macthesState(this, value); } : (value: string) => boolean | ||
> : ^ ^^ ^^^^^ | ||
>isState(prevConfig) : boolean | ||
> : ^^^^^^^ | ||
>isState : (state: unknown) => state is State | ||
> : ^ ^^ ^^^^^ | ||
>prevConfig : unknown | ||
> : ^^^^^^^ | ||
|
||
? prevConfig.matches | ||
>prevConfig.matches : (value: string) => boolean | ||
> : ^ ^^ ^^^^^ | ||
>prevConfig : State | ||
> : ^^^^^ | ||
>matches : (value: string) => boolean | ||
> : ^ ^^ ^^^^^ | ||
|
||
: function (value: string) { | ||
>function (value: string) { return macthesState(this, value); } : (value: string) => boolean | ||
> : ^ ^^ ^^^^^^^^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
|
||
return macthesState(this, value); | ||
>macthesState(this, value) : boolean | ||
> : ^^^^^^^ | ||
>macthesState : (state: { value: string; }, value: string) => boolean | ||
> : ^ ^^ ^^ ^^ ^^^^^ | ||
>this : { matches: (value: string) => boolean; value: string; } | ||
> : ^^^^^^^^^^^^ ^^ ^^^^^ ^^^^^^^^^ ^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
|
||
}, | ||
}; | ||
} | ||
|
||
return config; | ||
>config : unknown | ||
> : ^^^^^^^ | ||
} | ||
|
||
function test2(config: State) { | ||
>test2 : (config: State) => { matches: (value: string) => boolean; value: string; } | ||
> : ^ ^^ ^^^^^^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^ | ||
>config : State | ||
> : ^^^^^ | ||
|
||
return { | ||
>{ ...config, matches: function (value: string) { return macthesState(this, value); }, } : { matches: (value: string) => boolean; value: string; } | ||
> : ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^ | ||
|
||
...config, | ||
>config : State | ||
> : ^^^^^ | ||
|
||
matches: function (value: string) { | ||
>matches : (value: string) => boolean | ||
> : ^ ^^ ^^^^^^^^^^^^ | ||
>function (value: string) { return macthesState(this, value); } : (value: string) => boolean | ||
> : ^ ^^ ^^^^^^^^^^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
|
||
return macthesState(this, value); | ||
>macthesState(this, value) : boolean | ||
> : ^^^^^^^ | ||
>macthesState : (state: { value: string; }, value: string) => boolean | ||
> : ^ ^^ ^^ ^^ ^^^^^ | ||
>this : { matches: (value: string) => boolean; value: string; } | ||
> : ^^^^^^^^^^^^ ^^ ^^^^^^^^^^^^^^^^^^^^^ ^^^ | ||
>value : string | ||
> : ^^^^^^ | ||
|
||
}, | ||
}; | ||
} |
36 changes: 36 additions & 0 deletions
36
tests/cases/conformance/expressions/thisKeyword/thisInObjectLiterals2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// @strict: true | ||
// @noEmit: true | ||
|
||
// https://github.com/microsoft/TypeScript/issues/54723 | ||
|
||
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; | ||
|
||
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; | ||
} | ||
|
||
function test2(config: State) { | ||
return { | ||
...config, | ||
matches: function (value: string) { | ||
return macthesState(this, value); | ||
}, | ||
}; | ||
} |