Skip to content

Commit

Permalink
For union or intersection types use constituent serialized type if it…
Browse files Browse the repository at this point in the history
…s same for all of the constituent types

Fixes #10809
  • Loading branch information
sheetalkamat committed Oct 5, 2016
1 parent c40234f commit 40c2a53
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
35 changes: 33 additions & 2 deletions src/compiler/transformers/ts.ts
Expand Up @@ -1801,10 +1801,41 @@ namespace ts {
case SyntaxKind.TypeReference:
return serializeTypeReferenceNode(<TypeReferenceNode>node);

case SyntaxKind.IntersectionType:
case SyntaxKind.UnionType:
{
const unionOrIntersection = <UnionOrIntersectionTypeNode>node;
let serializedUnion: Identifier;
for (const typeNode of unionOrIntersection.types) {
const serializedIndividual = serializeTypeNode(typeNode) as Identifier;
// Non identifier
if (serializedIndividual.kind !== SyntaxKind.Identifier) {
serializedUnion = undefined;
break;
}

// One of the individual is global object, return immediately
if (serializedIndividual.text === "Object") {
return serializedIndividual;
}

// Different types
if (serializedUnion && serializedUnion.text !== serializedIndividual.text) {
serializedUnion = undefined;
break;
}

serializedUnion = serializedIndividual;
}

// If we were able to find common type
if (serializedUnion) {
return serializedUnion;
}
}
// Fallthrough
case SyntaxKind.TypeQuery:
case SyntaxKind.TypeLiteral:
case SyntaxKind.UnionType:
case SyntaxKind.IntersectionType:
case SyntaxKind.AnyKeyword:
case SyntaxKind.ThisType:
break;
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/metadataOfStringLiteral.js
Expand Up @@ -24,5 +24,5 @@ var Foo = (function () {
}());
__decorate([
PropDeco,
__metadata("design:type", Object)
__metadata("design:type", String)
], Foo.prototype, "foo");

2 comments on commit 40c2a53

@wizarrc
Copy link

@wizarrc wizarrc commented on 40c2a53 Dec 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sheetalkamat I'm having an issue with this code path when compiling inside of an aspnet core program using WebPack Middleware (UseWebpackDevMiddleware) for union types where one of the types is null. Is there a reason it's not handled and throws an assert? At this time it would be a pain to try and replicate the error, and I'm not really sure why it doesn't fail outside of the middleware, i.e. Visual Studio 2017 RC code editor and webpack command compile just fine, but fails at runtime when debugging inside a hacked (modified to add debugging) middleware using node-inspector. I tracked it down to this code commit where the assert is triggering due to SyntaxKind.NullKeyword (94) is not being handled.

@mhegazy
Copy link
Contributor

@mhegazy mhegazy commented on 40c2a53 Dec 5, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wizarrc, please file a new issue and share some repro code/steps to allow us to diagnose the issue better.

Please sign in to comment.