diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 91a8fae7a1f3d..97eace1d8e8cd 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1716,7 +1716,7 @@ namespace ts { } const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.Type & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false)); if (symbol && !(symbol.flags & SymbolFlags.NamespaceModule)) { - const message = (name === "Promise" || name === "Symbol") + const message = isES2015OrLaterConstructorName(name) ? Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_es2015_or_later : Diagnostics._0_only_refers_to_a_type_but_is_being_used_as_a_value_here; error(errorLocation, message, unescapeLeadingUnderscores(name)); @@ -1726,6 +1726,19 @@ namespace ts { return false; } + function isES2015OrLaterConstructorName(n: __String) { + switch (n) { + case "Promise": + case "Symbol": + case "Map": + case "WeakMap": + case "Set": + case "WeakSet": + return true; + } + return false; + } + function checkAndReportErrorForUsingNamespaceModuleAsValue(errorLocation: Node, name: __String, meaning: SymbolFlags): boolean { if (meaning & (SymbolFlags.Value & ~SymbolFlags.NamespaceModule & ~SymbolFlags.Type)) { const symbol = resolveSymbol(resolveName(errorLocation, name, SymbolFlags.NamespaceModule & ~SymbolFlags.Value, /*nameNotFoundMessage*/undefined, /*nameArg*/ undefined, /*isUse*/ false)); diff --git a/tests/baselines/reference/forwardDeclaredCommonTypes01.errors.txt b/tests/baselines/reference/forwardDeclaredCommonTypes01.errors.txt new file mode 100644 index 0000000000000..59a3a7313aa1d --- /dev/null +++ b/tests/baselines/reference/forwardDeclaredCommonTypes01.errors.txt @@ -0,0 +1,40 @@ +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(9,9): error TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(10,9): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(10,17): error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(11,9): error TS2585: 'Map' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(12,9): error TS2585: 'WeakMap' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(13,9): error TS2585: 'Set' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. +tests/cases/compiler/forwardDeclaredCommonTypes01.ts(14,9): error TS2585: 'WeakSet' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + + +==== tests/cases/compiler/forwardDeclaredCommonTypes01.ts (7 errors) ==== + interface Promise {} + interface Symbol {} + interface Map {} + interface WeakMap {} + interface Set {} + interface WeakSet {} + + (function() { + new Promise; + ~~~~~~~ +!!! error TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + new Symbol; Symbol(); + ~~~~~~ +!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + ~~~~~~ +!!! error TS2585: 'Symbol' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + new Map; + ~~~ +!!! error TS2585: 'Map' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + new WeakMap; + ~~~~~~~ +!!! error TS2585: 'WeakMap' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + new Set; + ~~~ +!!! error TS2585: 'Set' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + new WeakSet; + ~~~~~~~ +!!! error TS2585: 'WeakSet' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later. + }); + \ No newline at end of file diff --git a/tests/baselines/reference/forwardDeclaredCommonTypes01.js b/tests/baselines/reference/forwardDeclaredCommonTypes01.js new file mode 100644 index 0000000000000..5177852443034 --- /dev/null +++ b/tests/baselines/reference/forwardDeclaredCommonTypes01.js @@ -0,0 +1,28 @@ +//// [forwardDeclaredCommonTypes01.ts] +interface Promise {} +interface Symbol {} +interface Map {} +interface WeakMap {} +interface Set {} +interface WeakSet {} + +(function() { + new Promise; + new Symbol; Symbol(); + new Map; + new WeakMap; + new Set; + new WeakSet; +}); + + +//// [forwardDeclaredCommonTypes01.js] +(function () { + new Promise; + new Symbol; + Symbol(); + new Map; + new WeakMap; + new Set; + new WeakSet; +}); diff --git a/tests/baselines/reference/forwardDeclaredCommonTypes01.symbols b/tests/baselines/reference/forwardDeclaredCommonTypes01.symbols new file mode 100644 index 0000000000000..427bf4c39fb61 --- /dev/null +++ b/tests/baselines/reference/forwardDeclaredCommonTypes01.symbols @@ -0,0 +1,35 @@ +=== tests/cases/compiler/forwardDeclaredCommonTypes01.ts === +interface Promise {} +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(forwardDeclaredCommonTypes01.ts, 0, 0)) +>T : Symbol(T, Decl(lib.es5.d.ts, --, --), Decl(forwardDeclaredCommonTypes01.ts, 0, 18)) + +interface Symbol {} +>Symbol : Symbol(Symbol, Decl(lib.es5.d.ts, --, --), Decl(forwardDeclaredCommonTypes01.ts, 0, 23)) + +interface Map {} +>Map : Symbol(Map, Decl(forwardDeclaredCommonTypes01.ts, 1, 19)) +>K : Symbol(K, Decl(forwardDeclaredCommonTypes01.ts, 2, 14)) +>V : Symbol(V, Decl(forwardDeclaredCommonTypes01.ts, 2, 16)) + +interface WeakMap {} +>WeakMap : Symbol(WeakMap, Decl(forwardDeclaredCommonTypes01.ts, 2, 22)) +>K : Symbol(K, Decl(forwardDeclaredCommonTypes01.ts, 3, 18)) +>V : Symbol(V, Decl(forwardDeclaredCommonTypes01.ts, 3, 35)) + +interface Set {} +>Set : Symbol(Set, Decl(forwardDeclaredCommonTypes01.ts, 3, 41)) +>T : Symbol(T, Decl(forwardDeclaredCommonTypes01.ts, 4, 14)) + +interface WeakSet {} +>WeakSet : Symbol(WeakSet, Decl(forwardDeclaredCommonTypes01.ts, 4, 19)) +>T : Symbol(T, Decl(forwardDeclaredCommonTypes01.ts, 5, 18)) + +(function() { + new Promise; + new Symbol; Symbol(); + new Map; + new WeakMap; + new Set; + new WeakSet; +}); + diff --git a/tests/baselines/reference/forwardDeclaredCommonTypes01.types b/tests/baselines/reference/forwardDeclaredCommonTypes01.types new file mode 100644 index 0000000000000..279b772d7d579 --- /dev/null +++ b/tests/baselines/reference/forwardDeclaredCommonTypes01.types @@ -0,0 +1,40 @@ +=== tests/cases/compiler/forwardDeclaredCommonTypes01.ts === +interface Promise {} +interface Symbol {} +interface Map {} +interface WeakMap {} +interface Set {} +interface WeakSet {} + +(function() { +>(function() { new Promise; new Symbol; Symbol(); new Map; new WeakMap; new Set; new WeakSet;}) : () => void +>function() { new Promise; new Symbol; Symbol(); new Map; new WeakMap; new Set; new WeakSet;} : () => void + + new Promise; +>new Promise : any +>Promise : any + + new Symbol; Symbol(); +>new Symbol : any +>Symbol : any +>Symbol() : any +>Symbol : any + + new Map; +>new Map : any +>Map : any + + new WeakMap; +>new WeakMap : any +>WeakMap : any + + new Set; +>new Set : any +>Set : any + + new WeakSet; +>new WeakSet : any +>WeakSet : any + +}); + diff --git a/tests/cases/compiler/forwardDeclaredCommonTypes01.ts b/tests/cases/compiler/forwardDeclaredCommonTypes01.ts new file mode 100644 index 0000000000000..f4eea80ab0339 --- /dev/null +++ b/tests/cases/compiler/forwardDeclaredCommonTypes01.ts @@ -0,0 +1,18 @@ +// @lib: es5 +// @target: es5 + +interface Promise {} +interface Symbol {} +interface Map {} +interface WeakMap {} +interface Set {} +interface WeakSet {} + +(function() { + new Promise; + new Symbol; Symbol(); + new Map; + new WeakMap; + new Set; + new WeakSet; +});