Skip to content

Commit 40c2a53

Browse files
committed
For union or intersection types use constituent serialized type if its same for all of the constituent types
Fixes #10809
1 parent c40234f commit 40c2a53

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/compiler/transformers/ts.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,10 +1801,41 @@ namespace ts {
18011801
case SyntaxKind.TypeReference:
18021802
return serializeTypeReferenceNode(<TypeReferenceNode>node);
18031803

1804+
case SyntaxKind.IntersectionType:
1805+
case SyntaxKind.UnionType:
1806+
{
1807+
const unionOrIntersection = <UnionOrIntersectionTypeNode>node;
1808+
let serializedUnion: Identifier;
1809+
for (const typeNode of unionOrIntersection.types) {
1810+
const serializedIndividual = serializeTypeNode(typeNode) as Identifier;
1811+
// Non identifier
1812+
if (serializedIndividual.kind !== SyntaxKind.Identifier) {
1813+
serializedUnion = undefined;
1814+
break;
1815+
}
1816+
1817+
// One of the individual is global object, return immediately
1818+
if (serializedIndividual.text === "Object") {
1819+
return serializedIndividual;
1820+
}
1821+
1822+
// Different types
1823+
if (serializedUnion && serializedUnion.text !== serializedIndividual.text) {
1824+
serializedUnion = undefined;
1825+
break;
1826+
}
1827+
1828+
serializedUnion = serializedIndividual;
1829+
}
1830+
1831+
// If we were able to find common type
1832+
if (serializedUnion) {
1833+
return serializedUnion;
1834+
}
1835+
}
1836+
// Fallthrough
18041837
case SyntaxKind.TypeQuery:
18051838
case SyntaxKind.TypeLiteral:
1806-
case SyntaxKind.UnionType:
1807-
case SyntaxKind.IntersectionType:
18081839
case SyntaxKind.AnyKeyword:
18091840
case SyntaxKind.ThisType:
18101841
break;

tests/baselines/reference/metadataOfStringLiteral.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ var Foo = (function () {
2424
}());
2525
__decorate([
2626
PropDeco,
27-
__metadata("design:type", Object)
27+
__metadata("design:type", String)
2828
], Foo.prototype, "foo");

0 commit comments

Comments
 (0)