Skip to content

Commit beef2eb

Browse files
committed
Merge pull request #1079 from Microsoft/showNarrowedType
Show narrowed type in quickInfo/completion entry
2 parents 9e2eb3b + 1237033 commit beef2eb

File tree

6 files changed

+97
-5
lines changed

6 files changed

+97
-5
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ module ts {
8585
checkProgram: checkProgram,
8686
emitFiles: invokeEmitter,
8787
getParentOfSymbol: getParentOfSymbol,
88-
getTypeOfSymbol: getTypeOfSymbol,
88+
getNarrowedTypeOfSymbol: getNarrowedTypeOfSymbol,
8989
getDeclaredTypeOfSymbol: getDeclaredTypeOfSymbol,
9090
getPropertiesOfType: getPropertiesOfType,
9191
getPropertyOfType: getPropertyOfType,

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,7 @@ module ts {
701701
checkProgram(): void;
702702
emitFiles(targetSourceFile?: SourceFile): EmitResult;
703703
getParentOfSymbol(symbol: Symbol): Symbol;
704-
getTypeOfSymbol(symbol: Symbol): Type;
704+
getNarrowedTypeOfSymbol(symbol: Symbol, node: Node): Type;
705705
getDeclaredTypeOfSymbol(symbol: Symbol): Type;
706706
getPropertiesOfType(type: Type): Symbol[];
707707
getPropertyOfType(type: Type, propertyName: string): Symbol;

src/services/services.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2689,14 +2689,13 @@ module ts {
26892689

26902690
var symbol = lookUp(activeCompletionSession.symbols, entryName);
26912691
if (symbol) {
2692-
var type = session.typeChecker.getTypeOfSymbol(symbol);
2693-
Debug.assert(type !== undefined, "Could not find type for symbol");
26942692
var completionEntry = createCompletionEntry(symbol, session.typeChecker);
26952693
// TODO(drosen): Right now we just permit *all* semantic meanings when calling 'getSymbolKind'
26962694
// which is permissible given that it is backwards compatible; but really we should consider
26972695
// passing the meaning for the node so that we don't report that a suggestion for a value is an interface.
26982696
// We COULD also just do what 'getSymbolModifiers' does, which is to use the first declaration.
26992697
var location = getTouchingPropertyName(sourceFile, position);
2698+
Debug.assert(session.typeChecker.getNarrowedTypeOfSymbol(symbol, location) !== undefined, "Could not find type for symbol");
27002699
var displayPartsDocumentationsAndSymbolKind = getSymbolDisplayPartsDocumentationAndSymbolKind(symbol, getSourceFile(filename), location, session.typeChecker, location, SemanticMeaning.All);
27012700
return {
27022701
name: entryName,
@@ -2868,7 +2867,7 @@ module ts {
28682867
symbolKind = ScriptElementKind.memberVariableElement;
28692868
}
28702869

2871-
var type = typeResolver.getTypeOfSymbol(symbol);
2870+
var type = typeResolver.getNarrowedTypeOfSymbol(symbol, location);
28722871
if (type) {
28732872
if (location.parent && location.parent.kind === SyntaxKind.PropertyAccess) {
28742873
var right = (<PropertyAccess>location.parent).right;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////function foo(strOrNum: string | number) {
4+
//// /*1*/
5+
//// if (typeof strOrNum === "number") {
6+
//// /*2*/
7+
//// }
8+
//// else {
9+
//// /*3*/
10+
//// }
11+
////}
12+
13+
goTo.marker('1');
14+
verify.completionListContains("strOrNum", "(parameter) strOrNum: string | number");
15+
16+
goTo.marker('2');
17+
verify.completionListContains("strOrNum", "(parameter) strOrNum: number");
18+
19+
goTo.marker('3');
20+
verify.completionListContains("strOrNum", "(parameter) strOrNum: string");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////function foo(strOrNum: string | number) {
4+
//// if (typeof /*1*/strOrNum === "number") {
5+
//// return /*2*/strOrNum;
6+
//// }
7+
//// else {
8+
//// return /*3*/strOrNum.length;
9+
//// }
10+
////}
11+
12+
goTo.marker('1');
13+
verify.quickInfoIs('(parameter) strOrNum: string | number');
14+
verify.completionListContains("strOrNum", "(parameter) strOrNum: string | number");
15+
16+
goTo.marker('2');
17+
verify.quickInfoIs('(parameter) strOrNum: number');
18+
verify.completionListContains("strOrNum", "(parameter) strOrNum: number");
19+
20+
goTo.marker('3');
21+
verify.quickInfoIs('(parameter) strOrNum: string');
22+
verify.completionListContains("strOrNum", "(parameter) strOrNum: string");
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// <reference path='fourslash.ts'/>
2+
3+
////var strOrNum: string | number;
4+
////module m {
5+
//// var nonExportedStrOrNum: string | number;
6+
//// export var exportedStrOrNum: string | number;
7+
//// var num: number;
8+
//// var str: string;
9+
//// if (typeof /*1*/nonExportedStrOrNum === "number") {
10+
//// num = /*2*/nonExportedStrOrNum;
11+
//// }
12+
//// else {
13+
//// str = /*3*/nonExportedStrOrNum.length;
14+
//// }
15+
//// if (typeof /*4*/exportedStrOrNum === "number") {
16+
//// strOrNum = /*5*/exportedStrOrNum;
17+
//// }
18+
//// else {
19+
//// strOrNum = /*6*/exportedStrOrNum;
20+
//// }
21+
////}
22+
////if (typeof m./*7*/exportedStrOrNum === "number") {
23+
//// strOrNum = m./*8*/exportedStrOrNum;
24+
////}
25+
////else {
26+
//// strOrNum = m./*9*/exportedStrOrNum;
27+
////}
28+
29+
goTo.marker('1');
30+
verify.quickInfoIs('(var) nonExportedStrOrNum: string | number');
31+
verify.completionListContains("nonExportedStrOrNum", "(var) nonExportedStrOrNum: string | number");
32+
33+
goTo.marker('2');
34+
verify.quickInfoIs('(var) nonExportedStrOrNum: number');
35+
verify.completionListContains("nonExportedStrOrNum", "(var) nonExportedStrOrNum: number");
36+
37+
goTo.marker('3');
38+
verify.quickInfoIs('(var) nonExportedStrOrNum: string');
39+
verify.completionListContains("nonExportedStrOrNum", "(var) nonExportedStrOrNum: string");
40+
41+
['4', '5', '6', '7', '8', '9'].forEach((marker, index, arr) => {
42+
goTo.marker(marker);
43+
verify.quickInfoIs('(var) m.exportedStrOrNum: string | number');
44+
verify.completionListContains("exportedStrOrNum", "(var) m.exportedStrOrNum: string | number");
45+
});
46+
47+
['7', '8', '9'].forEach((marker, index, arr) => {
48+
goTo.marker(marker);
49+
verify.quickInfoIs('(var) m.exportedStrOrNum: string | number');
50+
verify.memberListContains("exportedStrOrNum", "(var) m.exportedStrOrNum: string | number");
51+
});

0 commit comments

Comments
 (0)