mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-14 07:02:44 -05:00
Merge pull request #28485 from Microsoft/conditionalTypeDecorator
When serializing conditional types, use true and false type to determine emit
This commit is contained in:
@@ -24172,41 +24172,10 @@ namespace ts {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.IntersectionType:
|
||||
case SyntaxKind.UnionType:
|
||||
let commonEntityName: EntityName | undefined;
|
||||
for (let typeNode of (<UnionOrIntersectionTypeNode>node).types) {
|
||||
while (typeNode.kind === SyntaxKind.ParenthesizedType) {
|
||||
typeNode = (typeNode as ParenthesizedTypeNode).type; // Skip parens if need be
|
||||
}
|
||||
if (typeNode.kind === SyntaxKind.NeverKeyword) {
|
||||
continue; // Always elide `never` from the union/intersection if possible
|
||||
}
|
||||
if (!strictNullChecks && (typeNode.kind === SyntaxKind.NullKeyword || typeNode.kind === SyntaxKind.UndefinedKeyword)) {
|
||||
continue; // Elide null and undefined from unions for metadata, just like what we did prior to the implementation of strict null checks
|
||||
}
|
||||
const individualEntityName = getEntityNameForDecoratorMetadata(typeNode);
|
||||
if (!individualEntityName) {
|
||||
// Individual is something like string number
|
||||
// So it would be serialized to either that type or object
|
||||
// Safe to return here
|
||||
return undefined;
|
||||
}
|
||||
return getEntityNameForDecoratorMetadataFromTypeList((<UnionOrIntersectionTypeNode>node).types);
|
||||
|
||||
if (commonEntityName) {
|
||||
// Note this is in sync with the transformation that happens for type node.
|
||||
// Keep this in sync with serializeUnionOrIntersectionType
|
||||
// Verify if they refer to same entity and is identifier
|
||||
// return undefined if they dont match because we would emit object
|
||||
if (!isIdentifier(commonEntityName) ||
|
||||
!isIdentifier(individualEntityName) ||
|
||||
commonEntityName.escapedText !== individualEntityName.escapedText) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
else {
|
||||
commonEntityName = individualEntityName;
|
||||
}
|
||||
}
|
||||
return commonEntityName;
|
||||
case SyntaxKind.ConditionalType:
|
||||
return getEntityNameForDecoratorMetadataFromTypeList([(<ConditionalTypeNode>node).trueType, (<ConditionalTypeNode>node).falseType]);
|
||||
|
||||
case SyntaxKind.ParenthesizedType:
|
||||
return getEntityNameForDecoratorMetadata((<ParenthesizedTypeNode>node).type);
|
||||
@@ -24217,6 +24186,44 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
|
||||
function getEntityNameForDecoratorMetadataFromTypeList(types: ReadonlyArray<TypeNode>): EntityName | undefined {
|
||||
let commonEntityName: EntityName | undefined;
|
||||
for (let typeNode of types) {
|
||||
while (typeNode.kind === SyntaxKind.ParenthesizedType) {
|
||||
typeNode = (typeNode as ParenthesizedTypeNode).type; // Skip parens if need be
|
||||
}
|
||||
if (typeNode.kind === SyntaxKind.NeverKeyword) {
|
||||
continue; // Always elide `never` from the union/intersection if possible
|
||||
}
|
||||
if (!strictNullChecks && (typeNode.kind === SyntaxKind.NullKeyword || typeNode.kind === SyntaxKind.UndefinedKeyword)) {
|
||||
continue; // Elide null and undefined from unions for metadata, just like what we did prior to the implementation of strict null checks
|
||||
}
|
||||
const individualEntityName = getEntityNameForDecoratorMetadata(typeNode);
|
||||
if (!individualEntityName) {
|
||||
// Individual is something like string number
|
||||
// So it would be serialized to either that type or object
|
||||
// Safe to return here
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (commonEntityName) {
|
||||
// Note this is in sync with the transformation that happens for type node.
|
||||
// Keep this in sync with serializeUnionOrIntersectionType
|
||||
// Verify if they refer to same entity and is identifier
|
||||
// return undefined if they dont match because we would emit object
|
||||
if (!isIdentifier(commonEntityName) ||
|
||||
!isIdentifier(individualEntityName) ||
|
||||
commonEntityName.escapedText !== individualEntityName.escapedText) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
else {
|
||||
commonEntityName = individualEntityName;
|
||||
}
|
||||
}
|
||||
return commonEntityName;
|
||||
}
|
||||
|
||||
function getParameterTypeNodeForDecoratorCheck(node: ParameterDeclaration): TypeNode | undefined {
|
||||
const typeNode = getEffectiveTypeAnnotationNode(node);
|
||||
return isRestParameter(node) ? getRestParameterElementType(typeNode) : typeNode;
|
||||
|
||||
@@ -1928,7 +1928,10 @@ namespace ts {
|
||||
|
||||
case SyntaxKind.IntersectionType:
|
||||
case SyntaxKind.UnionType:
|
||||
return serializeUnionOrIntersectionType(<UnionOrIntersectionTypeNode>node);
|
||||
return serializeTypeList((<UnionOrIntersectionTypeNode>node).types);
|
||||
|
||||
case SyntaxKind.ConditionalType:
|
||||
return serializeTypeList([(<ConditionalTypeNode>node).trueType, (<ConditionalTypeNode>node).falseType]);
|
||||
|
||||
case SyntaxKind.TypeQuery:
|
||||
case SyntaxKind.TypeOperator:
|
||||
@@ -1941,6 +1944,7 @@ namespace ts {
|
||||
case SyntaxKind.ImportType:
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
return Debug.failBadSyntaxKind(node);
|
||||
}
|
||||
@@ -1948,11 +1952,11 @@ namespace ts {
|
||||
return createIdentifier("Object");
|
||||
}
|
||||
|
||||
function serializeUnionOrIntersectionType(node: UnionOrIntersectionTypeNode): SerializedTypeNode {
|
||||
function serializeTypeList(types: ReadonlyArray<TypeNode>): SerializedTypeNode {
|
||||
// Note when updating logic here also update getEntityNameForDecoratorMetadata
|
||||
// so that aliases can be marked as referenced
|
||||
let serializedUnion: SerializedTypeNode | undefined;
|
||||
for (let typeNode of node.types) {
|
||||
for (let typeNode of types) {
|
||||
while (typeNode.kind === SyntaxKind.ParenthesizedType) {
|
||||
typeNode = (typeNode as ParenthesizedTypeNode).type; // Skip parens if need be
|
||||
}
|
||||
@@ -1998,6 +2002,11 @@ namespace ts {
|
||||
const kind = resolver.getTypeReferenceSerializationKind(node.typeName, currentNameScope || currentLexicalScope);
|
||||
switch (kind) {
|
||||
case TypeReferenceSerializationKind.Unknown:
|
||||
// From conditional type type reference that cannot be resolved is Similar to any or unknown
|
||||
if (findAncestor(node, n => n.parent && isConditionalTypeNode(n.parent) && (n.parent.trueType === n || n.parent.falseType === n))) {
|
||||
return createIdentifier("Object");
|
||||
}
|
||||
|
||||
const serialized = serializeEntityNameAsExpressionFallback(node.typeName);
|
||||
const temp = createTempVariable(hoistVariableDeclaration);
|
||||
return createConditional(
|
||||
|
||||
Reference in New Issue
Block a user