From 0ad494cedbcbf1b4aa83932480932bf79409ff9d Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Wed, 14 Sep 2016 18:01:54 -0700 Subject: [PATCH 1/4] Remove require name collision check for ES6 modules and later --- src/compiler/checker.ts | 5 +++ .../reference/es6UseOfTopLevelRequire.js | 30 +++++++++++++++++ .../reference/es6UseOfTopLevelRequire.symbols | 28 ++++++++++++++++ .../reference/es6UseOfTopLevelRequire.types | 33 +++++++++++++++++++ .../cases/compiler/es6UseOfTopLevelRequire.ts | 16 +++++++++ 5 files changed, 112 insertions(+) create mode 100644 tests/baselines/reference/es6UseOfTopLevelRequire.js create mode 100644 tests/baselines/reference/es6UseOfTopLevelRequire.symbols create mode 100644 tests/baselines/reference/es6UseOfTopLevelRequire.types create mode 100644 tests/cases/compiler/es6UseOfTopLevelRequire.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index c82e0ece074..5606c6831ca 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -15554,6 +15554,11 @@ namespace ts { } function checkCollisionWithRequireExportsInGeneratedCode(node: Node, name: Identifier) { + // No need to check for require or exports for ES6 modules and later + if (modulekind >= ModuleKind.ES6) { + return; + } + if (!needCollisionCheckForIdentifier(node, name, "require") && !needCollisionCheckForIdentifier(node, name, "exports")) { return; } diff --git a/tests/baselines/reference/es6UseOfTopLevelRequire.js b/tests/baselines/reference/es6UseOfTopLevelRequire.js new file mode 100644 index 00000000000..fa7295df989 --- /dev/null +++ b/tests/baselines/reference/es6UseOfTopLevelRequire.js @@ -0,0 +1,30 @@ +//// [tests/cases/compiler/es6UseOfTopLevelRequire.ts] //// + +//// [b.ts] + +export default function require(s: string): void { +} + +//// [c.ts] +export const exports = 0; +export default exports; + +//// [a.ts] +import require from "./b" +require("arg"); + +import exports from "./c" +var x = exports + 2; + + +//// [b.js] +export default function require(s) { +} +//// [c.js] +export const exports = 0; +export default exports; +//// [a.js] +import require from "./b"; +require("arg"); +import exports from "./c"; +var x = exports + 2; diff --git a/tests/baselines/reference/es6UseOfTopLevelRequire.symbols b/tests/baselines/reference/es6UseOfTopLevelRequire.symbols new file mode 100644 index 00000000000..710bd675210 --- /dev/null +++ b/tests/baselines/reference/es6UseOfTopLevelRequire.symbols @@ -0,0 +1,28 @@ +=== tests/cases/compiler/a.ts === +import require from "./b" +>require : Symbol(require, Decl(a.ts, 0, 6)) + +require("arg"); +>require : Symbol(require, Decl(a.ts, 0, 6)) + +import exports from "./c" +>exports : Symbol(exports, Decl(a.ts, 3, 6)) + +var x = exports + 2; +>x : Symbol(x, Decl(a.ts, 4, 3)) +>exports : Symbol(exports, Decl(a.ts, 3, 6)) + +=== tests/cases/compiler/b.ts === + +export default function require(s: string): void { +>require : Symbol(require, Decl(b.ts, 0, 0)) +>s : Symbol(s, Decl(b.ts, 1, 32)) +} + +=== tests/cases/compiler/c.ts === +export const exports = 0; +>exports : Symbol(exports, Decl(c.ts, 0, 12)) + +export default exports; +>exports : Symbol(exports, Decl(c.ts, 0, 12)) + diff --git a/tests/baselines/reference/es6UseOfTopLevelRequire.types b/tests/baselines/reference/es6UseOfTopLevelRequire.types new file mode 100644 index 00000000000..06acc9c2b42 --- /dev/null +++ b/tests/baselines/reference/es6UseOfTopLevelRequire.types @@ -0,0 +1,33 @@ +=== tests/cases/compiler/a.ts === +import require from "./b" +>require : (s: string) => void + +require("arg"); +>require("arg") : void +>require : (s: string) => void +>"arg" : "arg" + +import exports from "./c" +>exports : 0 + +var x = exports + 2; +>x : number +>exports + 2 : number +>exports : 0 +>2 : 2 + +=== tests/cases/compiler/b.ts === + +export default function require(s: string): void { +>require : (s: string) => void +>s : string +} + +=== tests/cases/compiler/c.ts === +export const exports = 0; +>exports : 0 +>0 : 0 + +export default exports; +>exports : 0 + diff --git a/tests/cases/compiler/es6UseOfTopLevelRequire.ts b/tests/cases/compiler/es6UseOfTopLevelRequire.ts new file mode 100644 index 00000000000..0874498c962 --- /dev/null +++ b/tests/cases/compiler/es6UseOfTopLevelRequire.ts @@ -0,0 +1,16 @@ +// @target: ES6 + +// @filename: b.ts +export default function require(s: string): void { +} + +// @filename: c.ts +export const exports = 0; +export default exports; + +// @filename: a.ts +import require from "./b" +require("arg"); + +import exports from "./c" +var x = exports + 2; From c08e2bfd28517e6e58bce981aed7430dae34a2ea Mon Sep 17 00:00:00 2001 From: zhengbli Date: Mon, 26 Sep 2016 16:52:25 -0700 Subject: [PATCH 2/4] update dom libs --- src/lib/dom.generated.d.ts | 381 ++++++++++++++++--------------- src/lib/webworker.generated.d.ts | 12 +- 2 files changed, 197 insertions(+), 196 deletions(-) diff --git a/src/lib/dom.generated.d.ts b/src/lib/dom.generated.d.ts index ee657f64a65..b520b18ea1d 100644 --- a/src/lib/dom.generated.d.ts +++ b/src/lib/dom.generated.d.ts @@ -7536,11 +7536,12 @@ declare var HashChangeEvent: { interface History { readonly length: number; readonly state: any; - back(distance?: any): void; - forward(distance?: any): void; - go(delta?: any): void; - pushState(statedata: any, title?: string, url?: string): void; - replaceState(statedata: any, title?: string, url?: string): void; + scrollRestoration: ScrollRestoration; + back(): void; + forward(): void; + go(delta?: number): void; + pushState(data: any, title: string, url?: string | null): void; + replaceState(data: any, title: string, url?: string | null): void; } declare var History: { @@ -13112,7 +13113,6 @@ interface Window extends EventTarget, WindowTimers, WindowSessionStorage, Window addEventListener(type: "waiting", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "wheel", listener: (this: this, ev: WheelEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; - [index: number]: Window; } declare var Window: { @@ -13143,7 +13143,7 @@ declare var XMLDocument: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - onreadystatechange: (this: this, ev: ProgressEvent) => any; + onreadystatechange: (this: this, ev: Event) => any; readonly readyState: number; readonly response: any; readonly responseText: string; @@ -13170,13 +13170,13 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { readonly LOADING: number; readonly OPENED: number; readonly UNSENT: number; - addEventListener(type: "abort", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; - addEventListener(type: "error", listener: (this: this, ev: ErrorEvent) => any, useCapture?: boolean): void; - addEventListener(type: "load", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "abort", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "error", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "load", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "loadend", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "loadstart", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "loadstart", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "progress", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "readystatechange", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "readystatechange", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "timeout", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } @@ -13517,183 +13517,183 @@ interface NavigatorUserMedia { } interface NodeSelector { - querySelector(selectors: "a"): HTMLAnchorElement; - querySelector(selectors: "abbr"): HTMLElement; - querySelector(selectors: "acronym"): HTMLElement; - querySelector(selectors: "address"): HTMLElement; - querySelector(selectors: "applet"): HTMLAppletElement; - querySelector(selectors: "area"): HTMLAreaElement; - querySelector(selectors: "article"): HTMLElement; - querySelector(selectors: "aside"): HTMLElement; - querySelector(selectors: "audio"): HTMLAudioElement; - querySelector(selectors: "b"): HTMLElement; - querySelector(selectors: "base"): HTMLBaseElement; - querySelector(selectors: "basefont"): HTMLBaseFontElement; - querySelector(selectors: "bdo"): HTMLElement; - querySelector(selectors: "big"): HTMLElement; - querySelector(selectors: "blockquote"): HTMLQuoteElement; - querySelector(selectors: "body"): HTMLBodyElement; - querySelector(selectors: "br"): HTMLBRElement; - querySelector(selectors: "button"): HTMLButtonElement; - querySelector(selectors: "canvas"): HTMLCanvasElement; - querySelector(selectors: "caption"): HTMLTableCaptionElement; - querySelector(selectors: "center"): HTMLElement; - querySelector(selectors: "circle"): SVGCircleElement; - querySelector(selectors: "cite"): HTMLElement; - querySelector(selectors: "clippath"): SVGClipPathElement; - querySelector(selectors: "code"): HTMLElement; - querySelector(selectors: "col"): HTMLTableColElement; - querySelector(selectors: "colgroup"): HTMLTableColElement; - querySelector(selectors: "datalist"): HTMLDataListElement; - querySelector(selectors: "dd"): HTMLElement; - querySelector(selectors: "defs"): SVGDefsElement; - querySelector(selectors: "del"): HTMLModElement; - querySelector(selectors: "desc"): SVGDescElement; - querySelector(selectors: "dfn"): HTMLElement; - querySelector(selectors: "dir"): HTMLDirectoryElement; - querySelector(selectors: "div"): HTMLDivElement; - querySelector(selectors: "dl"): HTMLDListElement; - querySelector(selectors: "dt"): HTMLElement; - querySelector(selectors: "ellipse"): SVGEllipseElement; - querySelector(selectors: "em"): HTMLElement; - querySelector(selectors: "embed"): HTMLEmbedElement; - querySelector(selectors: "feblend"): SVGFEBlendElement; - querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement; - querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement; - querySelector(selectors: "fecomposite"): SVGFECompositeElement; - querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement; - querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement; - querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement; - querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement; - querySelector(selectors: "feflood"): SVGFEFloodElement; - querySelector(selectors: "fefunca"): SVGFEFuncAElement; - querySelector(selectors: "fefuncb"): SVGFEFuncBElement; - querySelector(selectors: "fefuncg"): SVGFEFuncGElement; - querySelector(selectors: "fefuncr"): SVGFEFuncRElement; - querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement; - querySelector(selectors: "feimage"): SVGFEImageElement; - querySelector(selectors: "femerge"): SVGFEMergeElement; - querySelector(selectors: "femergenode"): SVGFEMergeNodeElement; - querySelector(selectors: "femorphology"): SVGFEMorphologyElement; - querySelector(selectors: "feoffset"): SVGFEOffsetElement; - querySelector(selectors: "fepointlight"): SVGFEPointLightElement; - querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement; - querySelector(selectors: "fespotlight"): SVGFESpotLightElement; - querySelector(selectors: "fetile"): SVGFETileElement; - querySelector(selectors: "feturbulence"): SVGFETurbulenceElement; - querySelector(selectors: "fieldset"): HTMLFieldSetElement; - querySelector(selectors: "figcaption"): HTMLElement; - querySelector(selectors: "figure"): HTMLElement; - querySelector(selectors: "filter"): SVGFilterElement; - querySelector(selectors: "font"): HTMLFontElement; - querySelector(selectors: "footer"): HTMLElement; - querySelector(selectors: "foreignobject"): SVGForeignObjectElement; - querySelector(selectors: "form"): HTMLFormElement; - querySelector(selectors: "frame"): HTMLFrameElement; - querySelector(selectors: "frameset"): HTMLFrameSetElement; - querySelector(selectors: "g"): SVGGElement; - querySelector(selectors: "h1"): HTMLHeadingElement; - querySelector(selectors: "h2"): HTMLHeadingElement; - querySelector(selectors: "h3"): HTMLHeadingElement; - querySelector(selectors: "h4"): HTMLHeadingElement; - querySelector(selectors: "h5"): HTMLHeadingElement; - querySelector(selectors: "h6"): HTMLHeadingElement; - querySelector(selectors: "head"): HTMLHeadElement; - querySelector(selectors: "header"): HTMLElement; - querySelector(selectors: "hgroup"): HTMLElement; - querySelector(selectors: "hr"): HTMLHRElement; - querySelector(selectors: "html"): HTMLHtmlElement; - querySelector(selectors: "i"): HTMLElement; - querySelector(selectors: "iframe"): HTMLIFrameElement; - querySelector(selectors: "image"): SVGImageElement; - querySelector(selectors: "img"): HTMLImageElement; - querySelector(selectors: "input"): HTMLInputElement; - querySelector(selectors: "ins"): HTMLModElement; - querySelector(selectors: "isindex"): HTMLUnknownElement; - querySelector(selectors: "kbd"): HTMLElement; - querySelector(selectors: "keygen"): HTMLElement; - querySelector(selectors: "label"): HTMLLabelElement; - querySelector(selectors: "legend"): HTMLLegendElement; - querySelector(selectors: "li"): HTMLLIElement; - querySelector(selectors: "line"): SVGLineElement; - querySelector(selectors: "lineargradient"): SVGLinearGradientElement; - querySelector(selectors: "link"): HTMLLinkElement; - querySelector(selectors: "listing"): HTMLPreElement; - querySelector(selectors: "map"): HTMLMapElement; - querySelector(selectors: "mark"): HTMLElement; - querySelector(selectors: "marker"): SVGMarkerElement; - querySelector(selectors: "marquee"): HTMLMarqueeElement; - querySelector(selectors: "mask"): SVGMaskElement; - querySelector(selectors: "menu"): HTMLMenuElement; - querySelector(selectors: "meta"): HTMLMetaElement; - querySelector(selectors: "metadata"): SVGMetadataElement; - querySelector(selectors: "meter"): HTMLMeterElement; - querySelector(selectors: "nav"): HTMLElement; - querySelector(selectors: "nextid"): HTMLUnknownElement; - querySelector(selectors: "nobr"): HTMLElement; - querySelector(selectors: "noframes"): HTMLElement; - querySelector(selectors: "noscript"): HTMLElement; - querySelector(selectors: "object"): HTMLObjectElement; - querySelector(selectors: "ol"): HTMLOListElement; - querySelector(selectors: "optgroup"): HTMLOptGroupElement; - querySelector(selectors: "option"): HTMLOptionElement; - querySelector(selectors: "p"): HTMLParagraphElement; - querySelector(selectors: "param"): HTMLParamElement; - querySelector(selectors: "path"): SVGPathElement; - querySelector(selectors: "pattern"): SVGPatternElement; - querySelector(selectors: "picture"): HTMLPictureElement; - querySelector(selectors: "plaintext"): HTMLElement; - querySelector(selectors: "polygon"): SVGPolygonElement; - querySelector(selectors: "polyline"): SVGPolylineElement; - querySelector(selectors: "pre"): HTMLPreElement; - querySelector(selectors: "progress"): HTMLProgressElement; - querySelector(selectors: "q"): HTMLQuoteElement; - querySelector(selectors: "radialgradient"): SVGRadialGradientElement; - querySelector(selectors: "rect"): SVGRectElement; - querySelector(selectors: "rt"): HTMLElement; - querySelector(selectors: "ruby"): HTMLElement; - querySelector(selectors: "s"): HTMLElement; - querySelector(selectors: "samp"): HTMLElement; - querySelector(selectors: "script"): HTMLScriptElement; - querySelector(selectors: "section"): HTMLElement; - querySelector(selectors: "select"): HTMLSelectElement; - querySelector(selectors: "small"): HTMLElement; - querySelector(selectors: "source"): HTMLSourceElement; - querySelector(selectors: "span"): HTMLSpanElement; - querySelector(selectors: "stop"): SVGStopElement; - querySelector(selectors: "strike"): HTMLElement; - querySelector(selectors: "strong"): HTMLElement; - querySelector(selectors: "style"): HTMLStyleElement; - querySelector(selectors: "sub"): HTMLElement; - querySelector(selectors: "sup"): HTMLElement; - querySelector(selectors: "svg"): SVGSVGElement; - querySelector(selectors: "switch"): SVGSwitchElement; - querySelector(selectors: "symbol"): SVGSymbolElement; - querySelector(selectors: "table"): HTMLTableElement; - querySelector(selectors: "tbody"): HTMLTableSectionElement; - querySelector(selectors: "td"): HTMLTableDataCellElement; - querySelector(selectors: "template"): HTMLTemplateElement; - querySelector(selectors: "text"): SVGTextElement; - querySelector(selectors: "textpath"): SVGTextPathElement; - querySelector(selectors: "textarea"): HTMLTextAreaElement; - querySelector(selectors: "tfoot"): HTMLTableSectionElement; - querySelector(selectors: "th"): HTMLTableHeaderCellElement; - querySelector(selectors: "thead"): HTMLTableSectionElement; - querySelector(selectors: "title"): HTMLTitleElement; - querySelector(selectors: "tr"): HTMLTableRowElement; - querySelector(selectors: "track"): HTMLTrackElement; - querySelector(selectors: "tspan"): SVGTSpanElement; - querySelector(selectors: "tt"): HTMLElement; - querySelector(selectors: "u"): HTMLElement; - querySelector(selectors: "ul"): HTMLUListElement; - querySelector(selectors: "use"): SVGUseElement; - querySelector(selectors: "var"): HTMLElement; - querySelector(selectors: "video"): HTMLVideoElement; - querySelector(selectors: "view"): SVGViewElement; - querySelector(selectors: "wbr"): HTMLElement; - querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement; - querySelector(selectors: "xmp"): HTMLPreElement; - querySelector(selectors: string): Element; + querySelector(selectors: "a"): HTMLAnchorElement | null; + querySelector(selectors: "abbr"): HTMLElement | null; + querySelector(selectors: "acronym"): HTMLElement | null; + querySelector(selectors: "address"): HTMLElement | null; + querySelector(selectors: "applet"): HTMLAppletElement | null; + querySelector(selectors: "area"): HTMLAreaElement | null; + querySelector(selectors: "article"): HTMLElement | null; + querySelector(selectors: "aside"): HTMLElement | null; + querySelector(selectors: "audio"): HTMLAudioElement | null; + querySelector(selectors: "b"): HTMLElement | null; + querySelector(selectors: "base"): HTMLBaseElement | null; + querySelector(selectors: "basefont"): HTMLBaseFontElement | null; + querySelector(selectors: "bdo"): HTMLElement | null; + querySelector(selectors: "big"): HTMLElement | null; + querySelector(selectors: "blockquote"): HTMLQuoteElement | null; + querySelector(selectors: "body"): HTMLBodyElement | null; + querySelector(selectors: "br"): HTMLBRElement | null; + querySelector(selectors: "button"): HTMLButtonElement | null; + querySelector(selectors: "canvas"): HTMLCanvasElement | null; + querySelector(selectors: "caption"): HTMLTableCaptionElement | null; + querySelector(selectors: "center"): HTMLElement | null; + querySelector(selectors: "circle"): SVGCircleElement | null; + querySelector(selectors: "cite"): HTMLElement | null; + querySelector(selectors: "clippath"): SVGClipPathElement | null; + querySelector(selectors: "code"): HTMLElement | null; + querySelector(selectors: "col"): HTMLTableColElement | null; + querySelector(selectors: "colgroup"): HTMLTableColElement | null; + querySelector(selectors: "datalist"): HTMLDataListElement | null; + querySelector(selectors: "dd"): HTMLElement | null; + querySelector(selectors: "defs"): SVGDefsElement | null; + querySelector(selectors: "del"): HTMLModElement | null; + querySelector(selectors: "desc"): SVGDescElement | null; + querySelector(selectors: "dfn"): HTMLElement | null; + querySelector(selectors: "dir"): HTMLDirectoryElement | null; + querySelector(selectors: "div"): HTMLDivElement | null; + querySelector(selectors: "dl"): HTMLDListElement | null; + querySelector(selectors: "dt"): HTMLElement | null; + querySelector(selectors: "ellipse"): SVGEllipseElement | null; + querySelector(selectors: "em"): HTMLElement | null; + querySelector(selectors: "embed"): HTMLEmbedElement | null; + querySelector(selectors: "feblend"): SVGFEBlendElement | null; + querySelector(selectors: "fecolormatrix"): SVGFEColorMatrixElement | null; + querySelector(selectors: "fecomponenttransfer"): SVGFEComponentTransferElement | null; + querySelector(selectors: "fecomposite"): SVGFECompositeElement | null; + querySelector(selectors: "feconvolvematrix"): SVGFEConvolveMatrixElement | null; + querySelector(selectors: "fediffuselighting"): SVGFEDiffuseLightingElement | null; + querySelector(selectors: "fedisplacementmap"): SVGFEDisplacementMapElement | null; + querySelector(selectors: "fedistantlight"): SVGFEDistantLightElement | null; + querySelector(selectors: "feflood"): SVGFEFloodElement | null; + querySelector(selectors: "fefunca"): SVGFEFuncAElement | null; + querySelector(selectors: "fefuncb"): SVGFEFuncBElement | null; + querySelector(selectors: "fefuncg"): SVGFEFuncGElement | null; + querySelector(selectors: "fefuncr"): SVGFEFuncRElement | null; + querySelector(selectors: "fegaussianblur"): SVGFEGaussianBlurElement | null; + querySelector(selectors: "feimage"): SVGFEImageElement | null; + querySelector(selectors: "femerge"): SVGFEMergeElement | null; + querySelector(selectors: "femergenode"): SVGFEMergeNodeElement | null; + querySelector(selectors: "femorphology"): SVGFEMorphologyElement | null; + querySelector(selectors: "feoffset"): SVGFEOffsetElement | null; + querySelector(selectors: "fepointlight"): SVGFEPointLightElement | null; + querySelector(selectors: "fespecularlighting"): SVGFESpecularLightingElement | null; + querySelector(selectors: "fespotlight"): SVGFESpotLightElement | null; + querySelector(selectors: "fetile"): SVGFETileElement | null; + querySelector(selectors: "feturbulence"): SVGFETurbulenceElement | null; + querySelector(selectors: "fieldset"): HTMLFieldSetElement | null; + querySelector(selectors: "figcaption"): HTMLElement | null; + querySelector(selectors: "figure"): HTMLElement | null; + querySelector(selectors: "filter"): SVGFilterElement | null; + querySelector(selectors: "font"): HTMLFontElement | null; + querySelector(selectors: "footer"): HTMLElement | null; + querySelector(selectors: "foreignobject"): SVGForeignObjectElement | null; + querySelector(selectors: "form"): HTMLFormElement | null; + querySelector(selectors: "frame"): HTMLFrameElement | null; + querySelector(selectors: "frameset"): HTMLFrameSetElement | null; + querySelector(selectors: "g"): SVGGElement | null; + querySelector(selectors: "h1"): HTMLHeadingElement | null; + querySelector(selectors: "h2"): HTMLHeadingElement | null; + querySelector(selectors: "h3"): HTMLHeadingElement | null; + querySelector(selectors: "h4"): HTMLHeadingElement | null; + querySelector(selectors: "h5"): HTMLHeadingElement | null; + querySelector(selectors: "h6"): HTMLHeadingElement | null; + querySelector(selectors: "head"): HTMLHeadElement | null; + querySelector(selectors: "header"): HTMLElement | null; + querySelector(selectors: "hgroup"): HTMLElement | null; + querySelector(selectors: "hr"): HTMLHRElement | null; + querySelector(selectors: "html"): HTMLHtmlElement | null; + querySelector(selectors: "i"): HTMLElement | null; + querySelector(selectors: "iframe"): HTMLIFrameElement | null; + querySelector(selectors: "image"): SVGImageElement | null; + querySelector(selectors: "img"): HTMLImageElement | null; + querySelector(selectors: "input"): HTMLInputElement | null; + querySelector(selectors: "ins"): HTMLModElement | null; + querySelector(selectors: "isindex"): HTMLUnknownElement | null; + querySelector(selectors: "kbd"): HTMLElement | null; + querySelector(selectors: "keygen"): HTMLElement | null; + querySelector(selectors: "label"): HTMLLabelElement | null; + querySelector(selectors: "legend"): HTMLLegendElement | null; + querySelector(selectors: "li"): HTMLLIElement | null; + querySelector(selectors: "line"): SVGLineElement | null; + querySelector(selectors: "lineargradient"): SVGLinearGradientElement | null; + querySelector(selectors: "link"): HTMLLinkElement | null; + querySelector(selectors: "listing"): HTMLPreElement | null; + querySelector(selectors: "map"): HTMLMapElement | null; + querySelector(selectors: "mark"): HTMLElement | null; + querySelector(selectors: "marker"): SVGMarkerElement | null; + querySelector(selectors: "marquee"): HTMLMarqueeElement | null; + querySelector(selectors: "mask"): SVGMaskElement | null; + querySelector(selectors: "menu"): HTMLMenuElement | null; + querySelector(selectors: "meta"): HTMLMetaElement | null; + querySelector(selectors: "metadata"): SVGMetadataElement | null; + querySelector(selectors: "meter"): HTMLMeterElement | null; + querySelector(selectors: "nav"): HTMLElement | null; + querySelector(selectors: "nextid"): HTMLUnknownElement | null; + querySelector(selectors: "nobr"): HTMLElement | null; + querySelector(selectors: "noframes"): HTMLElement | null; + querySelector(selectors: "noscript"): HTMLElement | null; + querySelector(selectors: "object"): HTMLObjectElement | null; + querySelector(selectors: "ol"): HTMLOListElement | null; + querySelector(selectors: "optgroup"): HTMLOptGroupElement | null; + querySelector(selectors: "option"): HTMLOptionElement | null; + querySelector(selectors: "p"): HTMLParagraphElement | null; + querySelector(selectors: "param"): HTMLParamElement | null; + querySelector(selectors: "path"): SVGPathElement | null; + querySelector(selectors: "pattern"): SVGPatternElement | null; + querySelector(selectors: "picture"): HTMLPictureElement | null; + querySelector(selectors: "plaintext"): HTMLElement | null; + querySelector(selectors: "polygon"): SVGPolygonElement | null; + querySelector(selectors: "polyline"): SVGPolylineElement | null; + querySelector(selectors: "pre"): HTMLPreElement | null; + querySelector(selectors: "progress"): HTMLProgressElement | null; + querySelector(selectors: "q"): HTMLQuoteElement | null; + querySelector(selectors: "radialgradient"): SVGRadialGradientElement | null; + querySelector(selectors: "rect"): SVGRectElement | null; + querySelector(selectors: "rt"): HTMLElement | null; + querySelector(selectors: "ruby"): HTMLElement | null; + querySelector(selectors: "s"): HTMLElement | null; + querySelector(selectors: "samp"): HTMLElement | null; + querySelector(selectors: "script"): HTMLScriptElement | null; + querySelector(selectors: "section"): HTMLElement | null; + querySelector(selectors: "select"): HTMLSelectElement | null; + querySelector(selectors: "small"): HTMLElement | null; + querySelector(selectors: "source"): HTMLSourceElement | null; + querySelector(selectors: "span"): HTMLSpanElement | null; + querySelector(selectors: "stop"): SVGStopElement | null; + querySelector(selectors: "strike"): HTMLElement | null; + querySelector(selectors: "strong"): HTMLElement | null; + querySelector(selectors: "style"): HTMLStyleElement | null; + querySelector(selectors: "sub"): HTMLElement | null; + querySelector(selectors: "sup"): HTMLElement | null; + querySelector(selectors: "svg"): SVGSVGElement | null; + querySelector(selectors: "switch"): SVGSwitchElement | null; + querySelector(selectors: "symbol"): SVGSymbolElement | null; + querySelector(selectors: "table"): HTMLTableElement | null; + querySelector(selectors: "tbody"): HTMLTableSectionElement | null; + querySelector(selectors: "td"): HTMLTableDataCellElement | null; + querySelector(selectors: "template"): HTMLTemplateElement | null; + querySelector(selectors: "text"): SVGTextElement | null; + querySelector(selectors: "textpath"): SVGTextPathElement | null; + querySelector(selectors: "textarea"): HTMLTextAreaElement | null; + querySelector(selectors: "tfoot"): HTMLTableSectionElement | null; + querySelector(selectors: "th"): HTMLTableHeaderCellElement | null; + querySelector(selectors: "thead"): HTMLTableSectionElement | null; + querySelector(selectors: "title"): HTMLTitleElement | null; + querySelector(selectors: "tr"): HTMLTableRowElement | null; + querySelector(selectors: "track"): HTMLTrackElement | null; + querySelector(selectors: "tspan"): SVGTSpanElement | null; + querySelector(selectors: "tt"): HTMLElement | null; + querySelector(selectors: "u"): HTMLElement | null; + querySelector(selectors: "ul"): HTMLUListElement | null; + querySelector(selectors: "use"): SVGUseElement | null; + querySelector(selectors: "var"): HTMLElement | null; + querySelector(selectors: "video"): HTMLVideoElement | null; + querySelector(selectors: "view"): SVGViewElement | null; + querySelector(selectors: "wbr"): HTMLElement | null; + querySelector(selectors: "x-ms-webview"): MSHTMLWebViewElement | null; + querySelector(selectors: "xmp"): HTMLPreElement | null; + querySelector(selectors: string): Element | null; querySelectorAll(selectors: "a"): NodeListOf; querySelectorAll(selectors: "abbr"): NodeListOf; querySelectorAll(selectors: "acronym"): NodeListOf; @@ -14622,4 +14622,5 @@ type ScrollBehavior = "auto" | "instant" | "smooth"; type ScrollLogicalPosition = "start" | "center" | "end" | "nearest"; type IDBValidKey = number | string | Date | IDBArrayKey; type BufferSource = ArrayBuffer | ArrayBufferView; -type MouseWheelEvent = WheelEvent; \ No newline at end of file +type MouseWheelEvent = WheelEvent; +type ScrollRestoration = "auto" | "manual"; \ No newline at end of file diff --git a/src/lib/webworker.generated.d.ts b/src/lib/webworker.generated.d.ts index 483348f3f75..b543939ef0a 100644 --- a/src/lib/webworker.generated.d.ts +++ b/src/lib/webworker.generated.d.ts @@ -728,7 +728,7 @@ declare var Worker: { } interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { - onreadystatechange: (this: this, ev: ProgressEvent) => any; + onreadystatechange: (this: this, ev: Event) => any; readonly readyState: number; readonly response: any; readonly responseText: string; @@ -754,13 +754,13 @@ interface XMLHttpRequest extends EventTarget, XMLHttpRequestEventTarget { readonly LOADING: number; readonly OPENED: number; readonly UNSENT: number; - addEventListener(type: "abort", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; - addEventListener(type: "error", listener: (this: this, ev: ErrorEvent) => any, useCapture?: boolean): void; - addEventListener(type: "load", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "abort", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "error", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "load", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "loadend", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "loadstart", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; + addEventListener(type: "loadstart", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: "progress", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; - addEventListener(type: "readystatechange", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; + addEventListener(type: "readystatechange", listener: (this: this, ev: Event) => any, useCapture?: boolean): void; addEventListener(type: "timeout", listener: (this: this, ev: ProgressEvent) => any, useCapture?: boolean): void; addEventListener(type: string, listener: EventListenerOrEventListenerObject, useCapture?: boolean): void; } From 94a0daf2ea9261e22b8def1511034f14ca332a49 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 27 Sep 2016 16:03:12 -0700 Subject: [PATCH 3/4] Properly handle partially discriminated unions --- src/compiler/checker.ts | 66 +++++++++++++++++++++++------------------ src/compiler/types.ts | 3 +- 2 files changed, 39 insertions(+), 30 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 72d2ca92af0..638ff533e3e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -4366,7 +4366,7 @@ namespace ts { function getPropertiesOfUnionOrIntersectionType(type: UnionOrIntersectionType): Symbol[] { for (const current of type.types) { for (const prop of getPropertiesOfType(current)) { - getPropertyOfUnionOrIntersectionType(type, prop.name); + getUnionOrIntersectionProperty(type, prop.name); } // The properties of a union type are those that are present in all constituent types, so // we only need to check the properties of the first type @@ -4374,7 +4374,19 @@ namespace ts { break; } } - return type.resolvedProperties ? symbolsToArray(type.resolvedProperties) : emptyArray; + const props = type.resolvedProperties; + if (props) { + const result: Symbol[] = []; + for (const key in props) { + const prop = props[key]; + // We need to filter out partial properties in union types + if (!(prop.flags & SymbolFlags.SyntheticProperty && (prop).isPartial)) { + result.push(prop); + } + } + return result; + } + return emptyArray; } function getPropertiesOfType(type: Type): Symbol[] { @@ -4427,6 +4439,7 @@ namespace ts { // Flags we want to propagate to the result if they exist in all source symbols let commonFlags = (containingType.flags & TypeFlags.Intersection) ? SymbolFlags.Optional : SymbolFlags.None; let isReadonly = false; + let isPartial = false; for (const current of types) { const type = getApparentType(current); if (type !== unknownType) { @@ -4444,21 +4457,20 @@ namespace ts { } } else if (containingType.flags & TypeFlags.Union) { - // A union type requires the property to be present in all constituent types - return undefined; + isPartial = true; } } } if (!props) { return undefined; } - if (props.length === 1) { + if (props.length === 1 && !isPartial) { return props[0]; } const propTypes: Type[] = []; const declarations: Declaration[] = []; let commonType: Type = undefined; - let hasCommonType = true; + let hasNonUniformType = false; for (const prop of props) { if (prop.declarations) { addRange(declarations, prop.declarations); @@ -4468,25 +4480,26 @@ namespace ts { commonType = type; } else if (type !== commonType) { - hasCommonType = false; + hasNonUniformType = true; } - propTypes.push(getTypeOfSymbol(prop)); + propTypes.push(type); } - const result = createSymbol( - SymbolFlags.Property | - SymbolFlags.Transient | - SymbolFlags.SyntheticProperty | - commonFlags, - name); + const result = createSymbol(SymbolFlags.Property | SymbolFlags.Transient | SymbolFlags.SyntheticProperty | commonFlags, name); result.containingType = containingType; - result.hasCommonType = hasCommonType; + result.hasNonUniformType = hasNonUniformType; + result.isPartial = isPartial; result.declarations = declarations; result.isReadonly = isReadonly; result.type = containingType.flags & TypeFlags.Union ? getUnionType(propTypes) : getIntersectionType(propTypes); return result; } - function getPropertyOfUnionOrIntersectionType(type: UnionOrIntersectionType, name: string): Symbol { + // Return the symbol for a given property in a union or intersection type, or undefined if the property + // does not exist in any constituent type. Note that the returned property may only be present in some + // constituents, in which case the isPartial flag is set when the containing type is union type. We need + // these partial properties when identifying discriminant properties, but otherwise they are filtered out + // and do not appear to be present in the union type. + function getUnionOrIntersectionProperty(type: UnionOrIntersectionType, name: string): Symbol { const properties = type.resolvedProperties || (type.resolvedProperties = createMap()); let property = properties[name]; if (!property) { @@ -4498,6 +4511,12 @@ namespace ts { return property; } + function getPropertyOfUnionOrIntersectionType(type: UnionOrIntersectionType, name: string): Symbol { + const property = getUnionOrIntersectionProperty(type, name); + // We need to filter out partial properties in union types + return property && !(property.flags & SymbolFlags.SyntheticProperty && (property).isPartial) ? property : undefined; + } + /** * Return the symbol for the property with the given name in the given type. Creates synthetic union properties when * necessary, maps primitive types and type parameters are to their apparent types, and augments with properties from @@ -8078,21 +8097,10 @@ namespace ts { function isDiscriminantProperty(type: Type, name: string) { if (type && type.flags & TypeFlags.Union) { - let prop = getPropertyOfType(type, name); - if (!prop) { - // The type may be a union that includes nullable or primitive types. If filtering - // those out produces a different type, get the property from that type instead. - // Effectively, we're checking if this *could* be a discriminant property once nullable - // and primitive types are removed by other type guards. - const filteredType = getTypeWithFacts(type, TypeFacts.Discriminatable); - if (filteredType !== type && filteredType.flags & TypeFlags.Union) { - prop = getPropertyOfType(filteredType, name); - } - } + const prop = getUnionOrIntersectionProperty(type, name); if (prop && prop.flags & SymbolFlags.SyntheticProperty) { if ((prop).isDiscriminantProperty === undefined) { - (prop).isDiscriminantProperty = !(prop).hasCommonType && - isLiteralType(getTypeOfSymbol(prop)); + (prop).isDiscriminantProperty = (prop).hasNonUniformType && isLiteralType(getTypeOfSymbol(prop)); } return (prop).isDiscriminantProperty; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index af71846145f..ab48ae543c2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2289,7 +2289,8 @@ namespace ts { mapper?: TypeMapper; // Type mapper for instantiation alias referenced?: boolean; // True if alias symbol has been referenced as a value containingType?: UnionOrIntersectionType; // Containing union or intersection type for synthetic property - hasCommonType?: boolean; // True if constituents of synthetic property all have same type + hasNonUniformType?: boolean; // True if constituents have non-uniform types + isPartial?: boolean; // True if syntheric property of union type occurs in some but not all constituents isDiscriminantProperty?: boolean; // True if discriminant synthetic property resolvedExports?: SymbolTable; // Resolved exports of module exportsChecked?: boolean; // True if exports of external module have been checked From 8b26cedf317008099483060fcf4c336bc744cd3e Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 27 Sep 2016 16:03:57 -0700 Subject: [PATCH 4/4] Add regression tests --- .../partiallyDiscriminantedUnions.js | 77 +++++++++++ .../partiallyDiscriminantedUnions.symbols | 117 ++++++++++++++++ .../partiallyDiscriminantedUnions.types | 128 ++++++++++++++++++ .../compiler/partiallyDiscriminantedUnions.ts | 46 +++++++ 4 files changed, 368 insertions(+) create mode 100644 tests/baselines/reference/partiallyDiscriminantedUnions.js create mode 100644 tests/baselines/reference/partiallyDiscriminantedUnions.symbols create mode 100644 tests/baselines/reference/partiallyDiscriminantedUnions.types create mode 100644 tests/cases/compiler/partiallyDiscriminantedUnions.ts diff --git a/tests/baselines/reference/partiallyDiscriminantedUnions.js b/tests/baselines/reference/partiallyDiscriminantedUnions.js new file mode 100644 index 00000000000..4c1a0a77f53 --- /dev/null +++ b/tests/baselines/reference/partiallyDiscriminantedUnions.js @@ -0,0 +1,77 @@ +//// [partiallyDiscriminantedUnions.ts] +// Repro from #10586 + +interface A1 { + type: 'a'; + subtype: 1; +} + +interface A2 { + type: 'a'; + subtype: 2; + foo: number; +} + +interface B { + type: 'b'; +} + +type AB = A1 | A2 | B; + +const ab: AB = {}; + +if (ab.type === 'a') { + if (ab.subtype === 2) { + ab.foo; + } +} + +// Repro from #11185 + +class Square { kind: "square"; } +class Circle { kind: "circle"; } + +type Shape = Circle | Square; +type Shapes = Shape | Array; + +function isShape(s : Shapes): s is Shape { + return !Array.isArray(s); +} + +function fail(s: Shapes) { + if (isShape(s)) { + if (s.kind === "circle") { + let c: Circle = s; + } + } +} + +//// [partiallyDiscriminantedUnions.js] +// Repro from #10586 +var ab = {}; +if (ab.type === 'a') { + if (ab.subtype === 2) { + ab.foo; + } +} +// Repro from #11185 +var Square = (function () { + function Square() { + } + return Square; +}()); +var Circle = (function () { + function Circle() { + } + return Circle; +}()); +function isShape(s) { + return !Array.isArray(s); +} +function fail(s) { + if (isShape(s)) { + if (s.kind === "circle") { + var c = s; + } + } +} diff --git a/tests/baselines/reference/partiallyDiscriminantedUnions.symbols b/tests/baselines/reference/partiallyDiscriminantedUnions.symbols new file mode 100644 index 00000000000..01d6143b9b2 --- /dev/null +++ b/tests/baselines/reference/partiallyDiscriminantedUnions.symbols @@ -0,0 +1,117 @@ +=== tests/cases/compiler/partiallyDiscriminantedUnions.ts === +// Repro from #10586 + +interface A1 { +>A1 : Symbol(A1, Decl(partiallyDiscriminantedUnions.ts, 0, 0)) + + type: 'a'; +>type : Symbol(A1.type, Decl(partiallyDiscriminantedUnions.ts, 2, 14)) + + subtype: 1; +>subtype : Symbol(A1.subtype, Decl(partiallyDiscriminantedUnions.ts, 3, 14)) +} + +interface A2 { +>A2 : Symbol(A2, Decl(partiallyDiscriminantedUnions.ts, 5, 1)) + + type: 'a'; +>type : Symbol(A2.type, Decl(partiallyDiscriminantedUnions.ts, 7, 14)) + + subtype: 2; +>subtype : Symbol(A2.subtype, Decl(partiallyDiscriminantedUnions.ts, 8, 14)) + + foo: number; +>foo : Symbol(A2.foo, Decl(partiallyDiscriminantedUnions.ts, 9, 15)) +} + +interface B { +>B : Symbol(B, Decl(partiallyDiscriminantedUnions.ts, 11, 1)) + + type: 'b'; +>type : Symbol(B.type, Decl(partiallyDiscriminantedUnions.ts, 13, 13)) +} + +type AB = A1 | A2 | B; +>AB : Symbol(AB, Decl(partiallyDiscriminantedUnions.ts, 15, 1)) +>A1 : Symbol(A1, Decl(partiallyDiscriminantedUnions.ts, 0, 0)) +>A2 : Symbol(A2, Decl(partiallyDiscriminantedUnions.ts, 5, 1)) +>B : Symbol(B, Decl(partiallyDiscriminantedUnions.ts, 11, 1)) + +const ab: AB = {}; +>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5)) +>AB : Symbol(AB, Decl(partiallyDiscriminantedUnions.ts, 15, 1)) +>AB : Symbol(AB, Decl(partiallyDiscriminantedUnions.ts, 15, 1)) + +if (ab.type === 'a') { +>ab.type : Symbol(type, Decl(partiallyDiscriminantedUnions.ts, 2, 14), Decl(partiallyDiscriminantedUnions.ts, 7, 14), Decl(partiallyDiscriminantedUnions.ts, 13, 13)) +>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5)) +>type : Symbol(type, Decl(partiallyDiscriminantedUnions.ts, 2, 14), Decl(partiallyDiscriminantedUnions.ts, 7, 14), Decl(partiallyDiscriminantedUnions.ts, 13, 13)) + + if (ab.subtype === 2) { +>ab.subtype : Symbol(subtype, Decl(partiallyDiscriminantedUnions.ts, 3, 14), Decl(partiallyDiscriminantedUnions.ts, 8, 14)) +>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5)) +>subtype : Symbol(subtype, Decl(partiallyDiscriminantedUnions.ts, 3, 14), Decl(partiallyDiscriminantedUnions.ts, 8, 14)) + + ab.foo; +>ab.foo : Symbol(A2.foo, Decl(partiallyDiscriminantedUnions.ts, 9, 15)) +>ab : Symbol(ab, Decl(partiallyDiscriminantedUnions.ts, 19, 5)) +>foo : Symbol(A2.foo, Decl(partiallyDiscriminantedUnions.ts, 9, 15)) + } +} + +// Repro from #11185 + +class Square { kind: "square"; } +>Square : Symbol(Square, Decl(partiallyDiscriminantedUnions.ts, 25, 1)) +>kind : Symbol(Square.kind, Decl(partiallyDiscriminantedUnions.ts, 29, 14)) + +class Circle { kind: "circle"; } +>Circle : Symbol(Circle, Decl(partiallyDiscriminantedUnions.ts, 29, 32)) +>kind : Symbol(Circle.kind, Decl(partiallyDiscriminantedUnions.ts, 30, 14)) + +type Shape = Circle | Square; +>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32)) +>Circle : Symbol(Circle, Decl(partiallyDiscriminantedUnions.ts, 29, 32)) +>Square : Symbol(Square, Decl(partiallyDiscriminantedUnions.ts, 25, 1)) + +type Shapes = Shape | Array; +>Shapes : Symbol(Shapes, Decl(partiallyDiscriminantedUnions.ts, 32, 29)) +>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32)) +>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32)) + +function isShape(s : Shapes): s is Shape { +>isShape : Symbol(isShape, Decl(partiallyDiscriminantedUnions.ts, 33, 35)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 35, 17)) +>Shapes : Symbol(Shapes, Decl(partiallyDiscriminantedUnions.ts, 32, 29)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 35, 17)) +>Shape : Symbol(Shape, Decl(partiallyDiscriminantedUnions.ts, 30, 32)) + + return !Array.isArray(s); +>Array.isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, --, --)) +>Array : Symbol(Array, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --)) +>isArray : Symbol(ArrayConstructor.isArray, Decl(lib.d.ts, --, --)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 35, 17)) +} + +function fail(s: Shapes) { +>fail : Symbol(fail, Decl(partiallyDiscriminantedUnions.ts, 37, 1)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14)) +>Shapes : Symbol(Shapes, Decl(partiallyDiscriminantedUnions.ts, 32, 29)) + + if (isShape(s)) { +>isShape : Symbol(isShape, Decl(partiallyDiscriminantedUnions.ts, 33, 35)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14)) + + if (s.kind === "circle") { +>s.kind : Symbol(kind, Decl(partiallyDiscriminantedUnions.ts, 29, 14), Decl(partiallyDiscriminantedUnions.ts, 30, 14)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14)) +>kind : Symbol(kind, Decl(partiallyDiscriminantedUnions.ts, 29, 14), Decl(partiallyDiscriminantedUnions.ts, 30, 14)) + + let c: Circle = s; +>c : Symbol(c, Decl(partiallyDiscriminantedUnions.ts, 42, 15)) +>Circle : Symbol(Circle, Decl(partiallyDiscriminantedUnions.ts, 29, 32)) +>s : Symbol(s, Decl(partiallyDiscriminantedUnions.ts, 39, 14)) + } + } +} diff --git a/tests/baselines/reference/partiallyDiscriminantedUnions.types b/tests/baselines/reference/partiallyDiscriminantedUnions.types new file mode 100644 index 00000000000..911c949373b --- /dev/null +++ b/tests/baselines/reference/partiallyDiscriminantedUnions.types @@ -0,0 +1,128 @@ +=== tests/cases/compiler/partiallyDiscriminantedUnions.ts === +// Repro from #10586 + +interface A1 { +>A1 : A1 + + type: 'a'; +>type : "a" + + subtype: 1; +>subtype : 1 +} + +interface A2 { +>A2 : A2 + + type: 'a'; +>type : "a" + + subtype: 2; +>subtype : 2 + + foo: number; +>foo : number +} + +interface B { +>B : B + + type: 'b'; +>type : "b" +} + +type AB = A1 | A2 | B; +>AB : AB +>A1 : A1 +>A2 : A2 +>B : B + +const ab: AB = {}; +>ab : AB +>AB : AB +>{} : AB +>AB : AB +>{} : {} + +if (ab.type === 'a') { +>ab.type === 'a' : boolean +>ab.type : "a" | "b" +>ab : AB +>type : "a" | "b" +>'a' : "a" + + if (ab.subtype === 2) { +>ab.subtype === 2 : boolean +>ab.subtype : 1 | 2 +>ab : A1 | A2 +>subtype : 1 | 2 +>2 : 2 + + ab.foo; +>ab.foo : number +>ab : A2 +>foo : number + } +} + +// Repro from #11185 + +class Square { kind: "square"; } +>Square : Square +>kind : "square" + +class Circle { kind: "circle"; } +>Circle : Circle +>kind : "circle" + +type Shape = Circle | Square; +>Shape : Shape +>Circle : Circle +>Square : Square + +type Shapes = Shape | Array; +>Shapes : Shapes +>Shape : Shape +>Array : T[] +>Shape : Shape + +function isShape(s : Shapes): s is Shape { +>isShape : (s: Shapes) => s is Shape +>s : Shapes +>Shapes : Shapes +>s : any +>Shape : Shape + + return !Array.isArray(s); +>!Array.isArray(s) : boolean +>Array.isArray(s) : boolean +>Array.isArray : (arg: any) => arg is any[] +>Array : ArrayConstructor +>isArray : (arg: any) => arg is any[] +>s : Shapes +} + +function fail(s: Shapes) { +>fail : (s: Shapes) => void +>s : Shapes +>Shapes : Shapes + + if (isShape(s)) { +>isShape(s) : boolean +>isShape : (s: Shapes) => s is Shape +>s : Shapes + + if (s.kind === "circle") { +>s.kind === "circle" : boolean +>s.kind : "square" | "circle" +>s : Shape +>kind : "square" | "circle" +>"circle" : "circle" + + let c: Circle = s; +>c : Circle +>Circle : Circle +>s : Circle + } + } +} diff --git a/tests/cases/compiler/partiallyDiscriminantedUnions.ts b/tests/cases/compiler/partiallyDiscriminantedUnions.ts new file mode 100644 index 00000000000..b4f89951f4c --- /dev/null +++ b/tests/cases/compiler/partiallyDiscriminantedUnions.ts @@ -0,0 +1,46 @@ +// Repro from #10586 + +interface A1 { + type: 'a'; + subtype: 1; +} + +interface A2 { + type: 'a'; + subtype: 2; + foo: number; +} + +interface B { + type: 'b'; +} + +type AB = A1 | A2 | B; + +const ab: AB = {}; + +if (ab.type === 'a') { + if (ab.subtype === 2) { + ab.foo; + } +} + +// Repro from #11185 + +class Square { kind: "square"; } +class Circle { kind: "circle"; } + +type Shape = Circle | Square; +type Shapes = Shape | Array; + +function isShape(s : Shapes): s is Shape { + return !Array.isArray(s); +} + +function fail(s: Shapes) { + if (isShape(s)) { + if (s.kind === "circle") { + let c: Circle = s; + } + } +} \ No newline at end of file