Skip to content

Commit 2ce64f2

Browse files
committed
Create typeof's string literal union type once
This changes the print order of string literal unions slightly. I think this is because 'string', 'symbol', etc are added to a union very early on in the creation of the checker.
1 parent 01bc4ef commit 2ce64f2

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/compiler/checker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,6 @@ namespace ts {
326326
"object": TypeFacts.TypeofEQObject,
327327
"function": TypeFacts.TypeofEQFunction
328328
});
329-
330329
const typeofNEFacts = createMapFromTemplate({
331330
"string": TypeFacts.TypeofNEString,
332331
"number": TypeFacts.TypeofNENumber,
@@ -336,14 +335,14 @@ namespace ts {
336335
"object": TypeFacts.TypeofNEObject,
337336
"function": TypeFacts.TypeofNEFunction
338337
});
339-
340338
const typeofTypesByName = createMapFromTemplate<Type>({
341339
"string": stringType,
342340
"number": numberType,
343341
"boolean": booleanType,
344342
"symbol": esSymbolType,
345343
"undefined": undefinedType
346344
});
345+
const typeofType = createTypeofType();
347346

348347
let jsxElementType: Type;
349348
let _jsxNamespace: string;
@@ -1725,6 +1724,10 @@ namespace ts {
17251724
return type;
17261725
}
17271726

1727+
function createTypeofType() {
1728+
return getUnionType(arrayFromMap(typeofEQFacts.keys(), s => getLiteralTypeForText(TypeFlags.StringLiteral, s)));
1729+
}
1730+
17281731
// A reserved member name starts with two underscores, but the third character cannot be an underscore
17291732
// or the @ symbol. A third underscore indicates an escaped form of an identifer that started
17301733
// with at least two underscores. The @ character indicates that the name is denoted by a well known ES
@@ -14609,7 +14612,7 @@ namespace ts {
1460914612

1461014613
function checkTypeOfExpression(node: TypeOfExpression): Type {
1461114614
checkExpression(node.expression);
14612-
return getUnionType(arrayFromMap(typeofEQFacts.keys(), s => getLiteralTypeForText(TypeFlags.StringLiteral, s)));
14615+
return typeofType;
1461314616
}
1461414617

1461514618
function checkVoidExpression(node: VoidExpression): Type {

tests/baselines/reference/intersectionTypeNormalization.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,9 @@ function getValueAsString(value: IntersectionFail): string {
227227

228228
if (value.kind === 'int') {
229229
>value.kind === 'int' : boolean
230-
>value.kind : "int" | "string"
230+
>value.kind : "string" | "int"
231231
>value : IntersectionFail
232-
>kind : "int" | "string"
232+
>kind : "string" | "int"
233233
>'int' : "int"
234234

235235
return '' + value.num;

tests/baselines/reference/modularizeLibrary_Dom.iterable.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
for (const element of document.getElementsByTagName("a")) {
44
>element : HTMLAnchorElement
55
>document.getElementsByTagName("a") : NodeListOf<HTMLAnchorElement>
6-
>document.getElementsByTagName : { <K extends "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "circle" | "cite" | "clippath" | "code" | "col" | "colgroup" | "datalist" | "dd" | "defs" | "del" | "desc" | "dfn" | "dir" | "div" | "dl" | "dt" | "ellipse" | "em" | "embed" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "fieldset" | "figcaption" | "figure" | "filter" | "font" | "footer" | "foreignobject" | "form" | "frame" | "frameset" | "g" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "image" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "line" | "lineargradient" | "link" | "listing" | "map" | "mark" | "marker" | "marquee" | "mask" | "menu" | "meta" | "metadata" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "object" | "ol" | "optgroup" | "option" | "p" | "param" | "path" | "pattern" | "picture" | "plaintext" | "polygon" | "polyline" | "pre" | "progress" | "q" | "radialgradient" | "rect" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "small" | "source" | "span" | "stop" | "strike" | "strong" | "style" | "sub" | "sup" | "svg" | "switch" | "symbol" | "table" | "tbody" | "td" | "template" | "text" | "textpath" | "textarea" | "tfoot" | "th" | "thead" | "title" | "tr" | "track" | "tspan" | "tt" | "u" | "ul" | "use" | "var" | "video" | "view" | "wbr" | "x-ms-webview" | "xmp">(tagname: K): ElementListTagNameMap[K]; (tagname: string): NodeListOf<Element>; }
6+
>document.getElementsByTagName : { <K extends "symbol" | "object" | "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "circle" | "cite" | "clippath" | "code" | "col" | "colgroup" | "datalist" | "dd" | "defs" | "del" | "desc" | "dfn" | "dir" | "div" | "dl" | "dt" | "ellipse" | "em" | "embed" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "fieldset" | "figcaption" | "figure" | "filter" | "font" | "footer" | "foreignobject" | "form" | "frame" | "frameset" | "g" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "image" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "line" | "lineargradient" | "link" | "listing" | "map" | "mark" | "marker" | "marquee" | "mask" | "menu" | "meta" | "metadata" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "ol" | "optgroup" | "option" | "p" | "param" | "path" | "pattern" | "picture" | "plaintext" | "polygon" | "polyline" | "pre" | "progress" | "q" | "radialgradient" | "rect" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "small" | "source" | "span" | "stop" | "strike" | "strong" | "style" | "sub" | "sup" | "svg" | "switch" | "table" | "tbody" | "td" | "template" | "text" | "textpath" | "textarea" | "tfoot" | "th" | "thead" | "title" | "tr" | "track" | "tspan" | "tt" | "u" | "ul" | "use" | "var" | "video" | "view" | "wbr" | "x-ms-webview" | "xmp">(tagname: K): ElementListTagNameMap[K]; (tagname: string): NodeListOf<Element>; }
77
>document : Document
8-
>getElementsByTagName : { <K extends "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "circle" | "cite" | "clippath" | "code" | "col" | "colgroup" | "datalist" | "dd" | "defs" | "del" | "desc" | "dfn" | "dir" | "div" | "dl" | "dt" | "ellipse" | "em" | "embed" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "fieldset" | "figcaption" | "figure" | "filter" | "font" | "footer" | "foreignobject" | "form" | "frame" | "frameset" | "g" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "image" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "line" | "lineargradient" | "link" | "listing" | "map" | "mark" | "marker" | "marquee" | "mask" | "menu" | "meta" | "metadata" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "object" | "ol" | "optgroup" | "option" | "p" | "param" | "path" | "pattern" | "picture" | "plaintext" | "polygon" | "polyline" | "pre" | "progress" | "q" | "radialgradient" | "rect" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "small" | "source" | "span" | "stop" | "strike" | "strong" | "style" | "sub" | "sup" | "svg" | "switch" | "symbol" | "table" | "tbody" | "td" | "template" | "text" | "textpath" | "textarea" | "tfoot" | "th" | "thead" | "title" | "tr" | "track" | "tspan" | "tt" | "u" | "ul" | "use" | "var" | "video" | "view" | "wbr" | "x-ms-webview" | "xmp">(tagname: K): ElementListTagNameMap[K]; (tagname: string): NodeListOf<Element>; }
8+
>getElementsByTagName : { <K extends "symbol" | "object" | "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "circle" | "cite" | "clippath" | "code" | "col" | "colgroup" | "datalist" | "dd" | "defs" | "del" | "desc" | "dfn" | "dir" | "div" | "dl" | "dt" | "ellipse" | "em" | "embed" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "fieldset" | "figcaption" | "figure" | "filter" | "font" | "footer" | "foreignobject" | "form" | "frame" | "frameset" | "g" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "image" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "line" | "lineargradient" | "link" | "listing" | "map" | "mark" | "marker" | "marquee" | "mask" | "menu" | "meta" | "metadata" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "ol" | "optgroup" | "option" | "p" | "param" | "path" | "pattern" | "picture" | "plaintext" | "polygon" | "polyline" | "pre" | "progress" | "q" | "radialgradient" | "rect" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "small" | "source" | "span" | "stop" | "strike" | "strong" | "style" | "sub" | "sup" | "svg" | "switch" | "table" | "tbody" | "td" | "template" | "text" | "textpath" | "textarea" | "tfoot" | "th" | "thead" | "title" | "tr" | "track" | "tspan" | "tt" | "u" | "ul" | "use" | "var" | "video" | "view" | "wbr" | "x-ms-webview" | "xmp">(tagname: K): ElementListTagNameMap[K]; (tagname: string): NodeListOf<Element>; }
99
>"a" : "a"
1010

1111
element.href;

0 commit comments

Comments
 (0)