diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index bf5f46de2480c..1e9a421751ddb 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -880,6 +880,14 @@ namespace FourSlash { }); } + public verifyTypeAtLocation(markerName: string, type: string) { + const marker = this.getMarkerByName(markerName); + const file = this.getProgram().getSourceFile(marker.fileName)!; + const node = ts.findPrecedingToken(marker.position, file); + const checker = this.getProgram().getTypeChecker(); + assert.equal(checker.typeToString(checker.getTypeAtLocation(node)), type); + } + public verifyCompletionListContains(entryId: ts.Completions.CompletionEntryIdentifier, text?: string, documentation?: string, kind?: string | { kind?: string, kindModifiers?: string }, spanIndex?: number, hasAction?: boolean, options?: FourSlashInterface.VerifyCompletionListContainsOptions) { const completions = this.getCompletionListAtCaret(options); if (completions) { @@ -3989,6 +3997,10 @@ namespace FourSlashInterface { super(state); } + public typeAtLocation(markerName: string, type: string) { + this.state.verifyTypeAtLocation(markerName, type); + } + public completionsAt(markerName: string, completions: ReadonlyArray, options?: CompletionsAtOptions) { this.state.verifyCompletionsAt(markerName, completions, options); } diff --git a/tests/cases/fourslash/typeAtLocation.ts b/tests/cases/fourslash/typeAtLocation.ts new file mode 100644 index 0000000000000..c3f6cca2719dd --- /dev/null +++ b/tests/cases/fourslash/typeAtLocation.ts @@ -0,0 +1,9 @@ +/// + +// @strictNullChecks: true + +////function test(x: T) { +//// x/**/!; +////} + +verify.typeAtLocation("", "string | undefined"); // TODO: GH#21317: should be "T".