mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Merge pull request #24137 from Microsoft/fix23977
Optimize intersections of unions of unit types
This commit is contained in:
+33
-4
@@ -8400,7 +8400,7 @@ namespace ts {
|
||||
includes & TypeFlags.Undefined ? includes & TypeFlags.NonWideningType ? undefinedType : undefinedWideningType :
|
||||
neverType;
|
||||
}
|
||||
return getUnionTypeFromSortedList(typeSet, aliasSymbol, aliasTypeArguments);
|
||||
return getUnionTypeFromSortedList(typeSet, includes & TypeFlags.NotUnit ? 0 : TypeFlags.UnionOfUnitTypes, aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
|
||||
function getUnionTypePredicate(signatures: ReadonlyArray<Signature>): TypePredicate {
|
||||
@@ -8440,7 +8440,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
// This function assumes the constituent type list is sorted and deduplicated.
|
||||
function getUnionTypeFromSortedList(types: Type[], aliasSymbol?: Symbol, aliasTypeArguments?: Type[]): Type {
|
||||
function getUnionTypeFromSortedList(types: Type[], unionOfUnitTypes: TypeFlags, aliasSymbol?: Symbol, aliasTypeArguments?: Type[]): Type {
|
||||
if (types.length === 0) {
|
||||
return neverType;
|
||||
}
|
||||
@@ -8451,7 +8451,7 @@ namespace ts {
|
||||
let type = unionTypes.get(id);
|
||||
if (!type) {
|
||||
const propagatedFlags = getPropagatingFlagsOfTypes(types, /*excludeKinds*/ TypeFlags.Nullable);
|
||||
type = <UnionType>createType(TypeFlags.Union | propagatedFlags);
|
||||
type = <UnionType>createType(TypeFlags.Union | propagatedFlags | unionOfUnitTypes);
|
||||
unionTypes.set(id, type);
|
||||
type.types = types;
|
||||
/*
|
||||
@@ -8523,6 +8523,29 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
// When intersecting unions of unit types we can simply intersect based on type identity.
|
||||
// Here we remove all unions of unit types from the given list and replace them with a
|
||||
// a single union containing an intersection of the unit types.
|
||||
function intersectUnionsOfUnitTypes(types: Type[]) {
|
||||
const unionIndex = findIndex(types, t => (t.flags & TypeFlags.UnionOfUnitTypes) !== 0);
|
||||
const unionType = <UnionType>types[unionIndex];
|
||||
let intersection = unionType.types;
|
||||
let i = types.length - 1;
|
||||
while (i > unionIndex) {
|
||||
const t = types[i];
|
||||
if (t.flags & TypeFlags.UnionOfUnitTypes) {
|
||||
intersection = filter(intersection, u => containsType((<UnionType>t).types, u));
|
||||
orderedRemoveItemAt(types, i);
|
||||
}
|
||||
i--;
|
||||
}
|
||||
if (intersection === unionType.types) {
|
||||
return false;
|
||||
}
|
||||
types[unionIndex] = getUnionTypeFromSortedList(intersection, unionType.flags & TypeFlags.UnionOfUnitTypes);
|
||||
return true;
|
||||
}
|
||||
|
||||
// We normalize combinations of intersection and union types based on the distributive property of the '&'
|
||||
// operator. Specifically, because X & (A | B) is equivalent to X & A | X & B, we can transform intersection
|
||||
// types with union type constituents into equivalent union types with intersection type constituents and
|
||||
@@ -8557,6 +8580,12 @@ namespace ts {
|
||||
return typeSet[0];
|
||||
}
|
||||
if (includes & TypeFlags.Union) {
|
||||
if (includes & TypeFlags.UnionOfUnitTypes && intersectUnionsOfUnitTypes(typeSet)) {
|
||||
// When the intersection creates a reduced set (which might mean that *all* union types have
|
||||
// disappeared), we restart the operation to get a new set of combined flags. Once we have
|
||||
// reduced we'll never reduce again, so this occurs at most once.
|
||||
return getIntersectionType(typeSet, aliasSymbol, aliasTypeArguments);
|
||||
}
|
||||
// We are attempting to construct a type of the form X & (A | B) & Y. Transform this into a type of
|
||||
// the form X & A & Y | X & B & Y and recursively reduce until no union type constituents remain.
|
||||
const unionIndex = findIndex(typeSet, t => (t.flags & TypeFlags.Union) !== 0);
|
||||
@@ -13331,7 +13360,7 @@ namespace ts {
|
||||
if (type.flags & TypeFlags.Union) {
|
||||
const types = (<UnionType>type).types;
|
||||
const filtered = filter(types, f);
|
||||
return filtered === types ? type : getUnionTypeFromSortedList(filtered);
|
||||
return filtered === types ? type : getUnionTypeFromSortedList(filtered, type.flags & TypeFlags.UnionOfUnitTypes);
|
||||
}
|
||||
return f(type) ? type : neverType;
|
||||
}
|
||||
|
||||
@@ -3692,6 +3692,8 @@ namespace ts {
|
||||
ContainsAnyFunctionType = 1 << 26, // Type is or contains the anyFunctionType
|
||||
NonPrimitive = 1 << 27, // intrinsic object type
|
||||
/* @internal */
|
||||
UnionOfUnitTypes = 1 << 28, // Type is union of unit types
|
||||
/* @internal */
|
||||
GenericMappedType = 1 << 29, // Flag used by maybeTypeOfKind
|
||||
|
||||
/* @internal */
|
||||
@@ -3729,6 +3731,8 @@ namespace ts {
|
||||
Narrowable = Any | StructuredOrInstantiable | StringLike | NumberLike | BooleanLike | ESSymbol | UniqueESSymbol | NonPrimitive,
|
||||
NotUnionOrUnit = Any | ESSymbol | Object | NonPrimitive,
|
||||
/* @internal */
|
||||
NotUnit = Any | String | Number | Boolean | Enum | ESSymbol | Void | Never | StructuredOrInstantiable,
|
||||
/* @internal */
|
||||
RequiresWidening = ContainsWideningType | ContainsObjectLiteral,
|
||||
/* @internal */
|
||||
PropagatingFlags = ContainsWideningType | ContainsObjectLiteral | ContainsAnyFunctionType,
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
tests/cases/compiler/intersectionsOfLargeUnions.ts(21,15): error TS2536: Type 'T' cannot be used to index type 'HTMLElementTagNameMap'.
|
||||
tests/cases/compiler/intersectionsOfLargeUnions.ts(21,15): error TS2536: Type 'P' cannot be used to index type 'HTMLElementTagNameMap[T]'.
|
||||
|
||||
|
||||
==== tests/cases/compiler/intersectionsOfLargeUnions.ts (2 errors) ====
|
||||
// Repro from #23977
|
||||
|
||||
export function assertIsElement(node: Node | null): node is Element {
|
||||
let nodeType = node === null ? null : node.nodeType;
|
||||
return nodeType === 1;
|
||||
}
|
||||
|
||||
export function assertNodeTagName<
|
||||
T extends keyof ElementTagNameMap,
|
||||
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
|
||||
if (assertIsElement(node)) {
|
||||
const nodeTagName = node.tagName.toLowerCase();
|
||||
return nodeTagName === tagName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function assertNodeProperty<
|
||||
T extends keyof ElementTagNameMap,
|
||||
P extends keyof ElementTagNameMap[T],
|
||||
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2536: Type 'T' cannot be used to index type 'HTMLElementTagNameMap'.
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
!!! error TS2536: Type 'P' cannot be used to index type 'HTMLElementTagNameMap[T]'.
|
||||
if (assertNodeTagName(node, tagName)) {
|
||||
node[prop];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
//// [intersectionsOfLargeUnions.ts]
|
||||
// Repro from #23977
|
||||
|
||||
export function assertIsElement(node: Node | null): node is Element {
|
||||
let nodeType = node === null ? null : node.nodeType;
|
||||
return nodeType === 1;
|
||||
}
|
||||
|
||||
export function assertNodeTagName<
|
||||
T extends keyof ElementTagNameMap,
|
||||
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
|
||||
if (assertIsElement(node)) {
|
||||
const nodeTagName = node.tagName.toLowerCase();
|
||||
return nodeTagName === tagName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function assertNodeProperty<
|
||||
T extends keyof ElementTagNameMap,
|
||||
P extends keyof ElementTagNameMap[T],
|
||||
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
|
||||
if (assertNodeTagName(node, tagName)) {
|
||||
node[prop];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//// [intersectionsOfLargeUnions.js]
|
||||
"use strict";
|
||||
// Repro from #23977
|
||||
exports.__esModule = true;
|
||||
function assertIsElement(node) {
|
||||
var nodeType = node === null ? null : node.nodeType;
|
||||
return nodeType === 1;
|
||||
}
|
||||
exports.assertIsElement = assertIsElement;
|
||||
function assertNodeTagName(node, tagName) {
|
||||
if (assertIsElement(node)) {
|
||||
var nodeTagName = node.tagName.toLowerCase();
|
||||
return nodeTagName === tagName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
exports.assertNodeTagName = assertNodeTagName;
|
||||
function assertNodeProperty(node, tagName, prop, value) {
|
||||
if (assertNodeTagName(node, tagName)) {
|
||||
node[prop];
|
||||
}
|
||||
}
|
||||
exports.assertNodeProperty = assertNodeProperty;
|
||||
@@ -0,0 +1,95 @@
|
||||
=== tests/cases/compiler/intersectionsOfLargeUnions.ts ===
|
||||
// Repro from #23977
|
||||
|
||||
export function assertIsElement(node: Node | null): node is Element {
|
||||
>assertIsElement : Symbol(assertIsElement, Decl(intersectionsOfLargeUnions.ts, 0, 0))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 2, 32))
|
||||
>Node : Symbol(Node, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 2, 32))
|
||||
>Element : Symbol(Element, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
|
||||
let nodeType = node === null ? null : node.nodeType;
|
||||
>nodeType : Symbol(nodeType, Decl(intersectionsOfLargeUnions.ts, 3, 7))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 2, 32))
|
||||
>node.nodeType : Symbol(Node.nodeType, Decl(lib.d.ts, --, --))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 2, 32))
|
||||
>nodeType : Symbol(Node.nodeType, Decl(lib.d.ts, --, --))
|
||||
|
||||
return nodeType === 1;
|
||||
>nodeType : Symbol(nodeType, Decl(intersectionsOfLargeUnions.ts, 3, 7))
|
||||
}
|
||||
|
||||
export function assertNodeTagName<
|
||||
>assertNodeTagName : Symbol(assertNodeTagName, Decl(intersectionsOfLargeUnions.ts, 5, 1))
|
||||
|
||||
T extends keyof ElementTagNameMap,
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 7, 34))
|
||||
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.d.ts, --, --))
|
||||
|
||||
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
|
||||
>U : Symbol(U, Decl(intersectionsOfLargeUnions.ts, 8, 38))
|
||||
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.d.ts, --, --))
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 7, 34))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 9, 36))
|
||||
>Node : Symbol(Node, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions.ts, 9, 54))
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 7, 34))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 9, 36))
|
||||
>U : Symbol(U, Decl(intersectionsOfLargeUnions.ts, 8, 38))
|
||||
|
||||
if (assertIsElement(node)) {
|
||||
>assertIsElement : Symbol(assertIsElement, Decl(intersectionsOfLargeUnions.ts, 0, 0))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 9, 36))
|
||||
|
||||
const nodeTagName = node.tagName.toLowerCase();
|
||||
>nodeTagName : Symbol(nodeTagName, Decl(intersectionsOfLargeUnions.ts, 11, 13))
|
||||
>node.tagName.toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --))
|
||||
>node.tagName : Symbol(Element.tagName, Decl(lib.d.ts, --, --))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 9, 36))
|
||||
>tagName : Symbol(Element.tagName, Decl(lib.d.ts, --, --))
|
||||
>toLowerCase : Symbol(String.toLowerCase, Decl(lib.d.ts, --, --))
|
||||
|
||||
return nodeTagName === tagName;
|
||||
>nodeTagName : Symbol(nodeTagName, Decl(intersectionsOfLargeUnions.ts, 11, 13))
|
||||
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions.ts, 9, 54))
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function assertNodeProperty<
|
||||
>assertNodeProperty : Symbol(assertNodeProperty, Decl(intersectionsOfLargeUnions.ts, 15, 1))
|
||||
|
||||
T extends keyof ElementTagNameMap,
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 17, 35))
|
||||
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.d.ts, --, --))
|
||||
|
||||
P extends keyof ElementTagNameMap[T],
|
||||
>P : Symbol(P, Decl(intersectionsOfLargeUnions.ts, 18, 38))
|
||||
>ElementTagNameMap : Symbol(ElementTagNameMap, Decl(lib.d.ts, --, --))
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 17, 35))
|
||||
|
||||
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
|
||||
>V : Symbol(V, Decl(intersectionsOfLargeUnions.ts, 19, 41))
|
||||
>HTMLElementTagNameMap : Symbol(HTMLElementTagNameMap, Decl(lib.d.ts, --, --))
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 17, 35))
|
||||
>P : Symbol(P, Decl(intersectionsOfLargeUnions.ts, 18, 38))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 20, 43))
|
||||
>Node : Symbol(Node, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
|
||||
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions.ts, 20, 61))
|
||||
>T : Symbol(T, Decl(intersectionsOfLargeUnions.ts, 17, 35))
|
||||
>prop : Symbol(prop, Decl(intersectionsOfLargeUnions.ts, 20, 73))
|
||||
>P : Symbol(P, Decl(intersectionsOfLargeUnions.ts, 18, 38))
|
||||
>value : Symbol(value, Decl(intersectionsOfLargeUnions.ts, 20, 82))
|
||||
>V : Symbol(V, Decl(intersectionsOfLargeUnions.ts, 19, 41))
|
||||
|
||||
if (assertNodeTagName(node, tagName)) {
|
||||
>assertNodeTagName : Symbol(assertNodeTagName, Decl(intersectionsOfLargeUnions.ts, 5, 1))
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 20, 43))
|
||||
>tagName : Symbol(tagName, Decl(intersectionsOfLargeUnions.ts, 20, 61))
|
||||
|
||||
node[prop];
|
||||
>node : Symbol(node, Decl(intersectionsOfLargeUnions.ts, 20, 43))
|
||||
>prop : Symbol(prop, Decl(intersectionsOfLargeUnions.ts, 20, 73))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
=== tests/cases/compiler/intersectionsOfLargeUnions.ts ===
|
||||
// Repro from #23977
|
||||
|
||||
export function assertIsElement(node: Node | null): node is Element {
|
||||
>assertIsElement : (node: Node | null) => node is Element
|
||||
>node : Node | null
|
||||
>Node : Node
|
||||
>null : null
|
||||
>node : any
|
||||
>Element : Element
|
||||
|
||||
let nodeType = node === null ? null : node.nodeType;
|
||||
>nodeType : number | null
|
||||
>node === null ? null : node.nodeType : number | null
|
||||
>node === null : boolean
|
||||
>node : Node | null
|
||||
>null : null
|
||||
>null : null
|
||||
>node.nodeType : number
|
||||
>node : Node
|
||||
>nodeType : number
|
||||
|
||||
return nodeType === 1;
|
||||
>nodeType === 1 : boolean
|
||||
>nodeType : number | null
|
||||
>1 : 1
|
||||
}
|
||||
|
||||
export function assertNodeTagName<
|
||||
>assertNodeTagName : <T extends "symbol" | "object" | "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "cite" | "code" | "col" | "colgroup" | "data" | "datalist" | "dd" | "del" | "dfn" | "dir" | "div" | "dl" | "dt" | "em" | "embed" | "fieldset" | "figcaption" | "figure" | "font" | "footer" | "form" | "frame" | "frameset" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "link" | "listing" | "map" | "mark" | "marquee" | "menu" | "meta" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "ol" | "optgroup" | "option" | "output" | "p" | "param" | "picture" | "plaintext" | "pre" | "progress" | "q" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "slot" | "small" | "source" | "span" | "strike" | "strong" | "style" | "sub" | "sup" | "table" | "tbody" | "td" | "template" | "textarea" | "tfoot" | "th" | "thead" | "time" | "title" | "tr" | "track" | "tt" | "u" | "ul" | "var" | "video" | "wbr" | "xmp" | "circle" | "clippath" | "defs" | "desc" | "ellipse" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "filter" | "foreignobject" | "g" | "image" | "line" | "lineargradient" | "marker" | "mask" | "metadata" | "path" | "pattern" | "polygon" | "polyline" | "radialgradient" | "rect" | "stop" | "svg" | "switch" | "text" | "textpath" | "tspan" | "use" | "view", U extends ElementTagNameMap[T]>(node: Node | null, tagName: T) => node is U
|
||||
|
||||
T extends keyof ElementTagNameMap,
|
||||
>T : T
|
||||
>ElementTagNameMap : ElementTagNameMap
|
||||
|
||||
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
|
||||
>U : U
|
||||
>ElementTagNameMap : ElementTagNameMap
|
||||
>T : T
|
||||
>node : Node | null
|
||||
>Node : Node
|
||||
>null : null
|
||||
>tagName : T
|
||||
>T : T
|
||||
>node : any
|
||||
>U : U
|
||||
|
||||
if (assertIsElement(node)) {
|
||||
>assertIsElement(node) : boolean
|
||||
>assertIsElement : (node: Node | null) => node is Element
|
||||
>node : Node | null
|
||||
|
||||
const nodeTagName = node.tagName.toLowerCase();
|
||||
>nodeTagName : string
|
||||
>node.tagName.toLowerCase() : string
|
||||
>node.tagName.toLowerCase : () => string
|
||||
>node.tagName : string
|
||||
>node : Element
|
||||
>tagName : string
|
||||
>toLowerCase : () => string
|
||||
|
||||
return nodeTagName === tagName;
|
||||
>nodeTagName === tagName : boolean
|
||||
>nodeTagName : string
|
||||
>tagName : T
|
||||
}
|
||||
return false;
|
||||
>false : false
|
||||
}
|
||||
|
||||
export function assertNodeProperty<
|
||||
>assertNodeProperty : <T extends "symbol" | "object" | "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "cite" | "code" | "col" | "colgroup" | "data" | "datalist" | "dd" | "del" | "dfn" | "dir" | "div" | "dl" | "dt" | "em" | "embed" | "fieldset" | "figcaption" | "figure" | "font" | "footer" | "form" | "frame" | "frameset" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "link" | "listing" | "map" | "mark" | "marquee" | "menu" | "meta" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "ol" | "optgroup" | "option" | "output" | "p" | "param" | "picture" | "plaintext" | "pre" | "progress" | "q" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "slot" | "small" | "source" | "span" | "strike" | "strong" | "style" | "sub" | "sup" | "table" | "tbody" | "td" | "template" | "textarea" | "tfoot" | "th" | "thead" | "time" | "title" | "tr" | "track" | "tt" | "u" | "ul" | "var" | "video" | "wbr" | "xmp" | "circle" | "clippath" | "defs" | "desc" | "ellipse" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "filter" | "foreignobject" | "g" | "image" | "line" | "lineargradient" | "marker" | "mask" | "metadata" | "path" | "pattern" | "polygon" | "polyline" | "radialgradient" | "rect" | "stop" | "svg" | "switch" | "text" | "textpath" | "tspan" | "use" | "view", P extends keyof ElementTagNameMap[T], V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) => void
|
||||
|
||||
T extends keyof ElementTagNameMap,
|
||||
>T : T
|
||||
>ElementTagNameMap : ElementTagNameMap
|
||||
|
||||
P extends keyof ElementTagNameMap[T],
|
||||
>P : P
|
||||
>ElementTagNameMap : ElementTagNameMap
|
||||
>T : T
|
||||
|
||||
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
|
||||
>V : V
|
||||
>HTMLElementTagNameMap : HTMLElementTagNameMap
|
||||
>T : T
|
||||
>P : P
|
||||
>node : Node | null
|
||||
>Node : Node
|
||||
>null : null
|
||||
>tagName : T
|
||||
>T : T
|
||||
>prop : P
|
||||
>P : P
|
||||
>value : V
|
||||
>V : V
|
||||
|
||||
if (assertNodeTagName(node, tagName)) {
|
||||
>assertNodeTagName(node, tagName) : boolean
|
||||
>assertNodeTagName : <T extends "symbol" | "object" | "a" | "abbr" | "acronym" | "address" | "applet" | "area" | "article" | "aside" | "audio" | "b" | "base" | "basefont" | "bdo" | "big" | "blockquote" | "body" | "br" | "button" | "canvas" | "caption" | "center" | "cite" | "code" | "col" | "colgroup" | "data" | "datalist" | "dd" | "del" | "dfn" | "dir" | "div" | "dl" | "dt" | "em" | "embed" | "fieldset" | "figcaption" | "figure" | "font" | "footer" | "form" | "frame" | "frameset" | "h1" | "h2" | "h3" | "h4" | "h5" | "h6" | "head" | "header" | "hgroup" | "hr" | "html" | "i" | "iframe" | "img" | "input" | "ins" | "isindex" | "kbd" | "keygen" | "label" | "legend" | "li" | "link" | "listing" | "map" | "mark" | "marquee" | "menu" | "meta" | "meter" | "nav" | "nextid" | "nobr" | "noframes" | "noscript" | "ol" | "optgroup" | "option" | "output" | "p" | "param" | "picture" | "plaintext" | "pre" | "progress" | "q" | "rt" | "ruby" | "s" | "samp" | "script" | "section" | "select" | "slot" | "small" | "source" | "span" | "strike" | "strong" | "style" | "sub" | "sup" | "table" | "tbody" | "td" | "template" | "textarea" | "tfoot" | "th" | "thead" | "time" | "title" | "tr" | "track" | "tt" | "u" | "ul" | "var" | "video" | "wbr" | "xmp" | "circle" | "clippath" | "defs" | "desc" | "ellipse" | "feblend" | "fecolormatrix" | "fecomponenttransfer" | "fecomposite" | "feconvolvematrix" | "fediffuselighting" | "fedisplacementmap" | "fedistantlight" | "feflood" | "fefunca" | "fefuncb" | "fefuncg" | "fefuncr" | "fegaussianblur" | "feimage" | "femerge" | "femergenode" | "femorphology" | "feoffset" | "fepointlight" | "fespecularlighting" | "fespotlight" | "fetile" | "feturbulence" | "filter" | "foreignobject" | "g" | "image" | "line" | "lineargradient" | "marker" | "mask" | "metadata" | "path" | "pattern" | "polygon" | "polyline" | "radialgradient" | "rect" | "stop" | "svg" | "switch" | "text" | "textpath" | "tspan" | "use" | "view", U extends ElementTagNameMap[T]>(node: Node | null, tagName: T) => node is U
|
||||
>node : Node | null
|
||||
>tagName : T
|
||||
|
||||
node[prop];
|
||||
>node[prop] : ElementTagNameMap[T][P]
|
||||
>node : ElementTagNameMap[T]
|
||||
>prop : P
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
// @strict: true
|
||||
|
||||
// Repro from #23977
|
||||
|
||||
export function assertIsElement(node: Node | null): node is Element {
|
||||
let nodeType = node === null ? null : node.nodeType;
|
||||
return nodeType === 1;
|
||||
}
|
||||
|
||||
export function assertNodeTagName<
|
||||
T extends keyof ElementTagNameMap,
|
||||
U extends ElementTagNameMap[T]>(node: Node | null, tagName: T): node is U {
|
||||
if (assertIsElement(node)) {
|
||||
const nodeTagName = node.tagName.toLowerCase();
|
||||
return nodeTagName === tagName;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
export function assertNodeProperty<
|
||||
T extends keyof ElementTagNameMap,
|
||||
P extends keyof ElementTagNameMap[T],
|
||||
V extends HTMLElementTagNameMap[T][P]>(node: Node | null, tagName: T, prop: P, value: V) {
|
||||
if (assertNodeTagName(node, tagName)) {
|
||||
node[prop];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user