Unescape string literal types starting with double underscore.

String literal types starting with double underscore are escaped in the
parser and need to be unescaped before the type is given the string
literal as its name.
This commit is contained in:
Nathan Shively-Sanders 2016-05-17 13:05:03 -07:00
parent 3fb16f5930
commit e25927da57
4 changed files with 24 additions and 4 deletions

View File

@ -5138,12 +5138,13 @@ namespace ts {
}
function getStringLiteralTypeForText(text: string): StringLiteralType {
if (hasProperty(stringLiteralTypes, text)) {
return stringLiteralTypes[text];
const unescaped = unescapeIdentifier(text);
if (hasProperty(stringLiteralTypes, unescaped)) {
return stringLiteralTypes[unescaped];
}
const type = stringLiteralTypes[text] = <StringLiteralType>createType(TypeFlags.StringLiteral);
type.text = text;
const type = stringLiteralTypes[unescaped] = <StringLiteralType>createType(TypeFlags.StringLiteral);
type.text = unescaped;
return type;
}

View File

@ -0,0 +1,9 @@
tests/cases/compiler/doubleUnderStringLiteralAssignability.ts(2,5): error TS2322: Type '"no_dunder"' is not assignable to type '"__dunder"'.
==== tests/cases/compiler/doubleUnderStringLiteralAssignability.ts (1 errors) ====
var shouldBeOk: '__dunder' = '__dunder';
var bad: '__dunder' = 'no_dunder';
~~~
!!! error TS2322: Type '"no_dunder"' is not assignable to type '"__dunder"'.

View File

@ -0,0 +1,8 @@
//// [doubleUnderStringLiteralAssignability.ts]
var shouldBeOk: '__dunder' = '__dunder';
var bad: '__dunder' = 'no_dunder';
//// [doubleUnderStringLiteralAssignability.js]
var shouldBeOk = '__dunder';
var bad = 'no_dunder';

View File

@ -0,0 +1,2 @@
var shouldBeOk: '__dunder' = '__dunder';
var bad: '__dunder' = 'no_dunder';