Skip to content

Use the value symbol for decorator purpose only if it is same as type symbol #13584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20518,18 +20518,21 @@ namespace ts {
function getTypeReferenceSerializationKind(typeName: EntityName, location?: Node): TypeReferenceSerializationKind {
// Resolve the symbol as a value to ensure the type can be reached at runtime during emit.
const valueSymbol = resolveEntityName(typeName, SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
const globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol();
if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) {
return TypeReferenceSerializationKind.Promise;
}

const constructorType = valueSymbol ? getTypeOfSymbol(valueSymbol) : undefined;
if (constructorType && isConstructorType(constructorType)) {
return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
}

// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
const typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
if (valueSymbol && valueSymbol === typeSymbol) {
Copy link

Choose a reason for hiding this comment

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

just wanna be enlightened about how Typescript type checker works:

if there's already a value called "Event" (like the DOM.Event class) and there is a type declared that is named "Event" and they are equal, we will assume that the reference is to the type rather than to the value?

Copy link
Contributor

Choose a reason for hiding this comment

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

declarations do merge in TS, see http://www.typescriptlang.org/docs/handbook/declaration-merging.html for more details.

const globalPromiseSymbol = tryGetGlobalPromiseConstructorSymbol();
if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) {
return TypeReferenceSerializationKind.Promise;
}

const constructorType = getTypeOfSymbol(valueSymbol);
if (constructorType && isConstructorType(constructorType)) {
return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
}
}

// We might not be able to resolve type symbol so use unknown type in that case (eg error case)
if (!typeSymbol) {
return TypeReferenceSerializationKind.ObjectType;
Expand Down
38 changes: 38 additions & 0 deletions tests/baselines/reference/metadataOfEventAlias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//// [tests/cases/compiler/metadataOfEventAlias.ts] ////

//// [event.ts]

export interface Event { title: string };

//// [test.ts]
import { Event } from './event';
function Input(target: any, key: string): void { }
export class SomeClass {
@Input event: Event;
}

//// [event.js]
"use strict";
;
//// [test.js]
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
function Input(target, key) { }
var SomeClass = (function () {
function SomeClass() {
}
return SomeClass;
}());
__decorate([
Input,
__metadata("design:type", Object)
], SomeClass.prototype, "event", void 0);
exports.SomeClass = SomeClass;
23 changes: 23 additions & 0 deletions tests/baselines/reference/metadataOfEventAlias.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/event.ts ===

export interface Event { title: string };
>Event : Symbol(Event, Decl(event.ts, 0, 0))
>title : Symbol(Event.title, Decl(event.ts, 1, 24))

=== tests/cases/compiler/test.ts ===
import { Event } from './event';
>Event : Symbol(Event, Decl(test.ts, 0, 8))

function Input(target: any, key: string): void { }
>Input : Symbol(Input, Decl(test.ts, 0, 32))
>target : Symbol(target, Decl(test.ts, 1, 15))
>key : Symbol(key, Decl(test.ts, 1, 27))

export class SomeClass {
>SomeClass : Symbol(SomeClass, Decl(test.ts, 1, 50))

@Input event: Event;
>Input : Symbol(Input, Decl(test.ts, 0, 32))
>event : Symbol(SomeClass.event, Decl(test.ts, 2, 24))
>Event : Symbol(Event, Decl(test.ts, 0, 8))
}
23 changes: 23 additions & 0 deletions tests/baselines/reference/metadataOfEventAlias.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
=== tests/cases/compiler/event.ts ===

export interface Event { title: string };
>Event : Event
>title : string

=== tests/cases/compiler/test.ts ===
import { Event } from './event';
>Event : any

function Input(target: any, key: string): void { }
>Input : (target: any, key: string) => void
>target : any
>key : string

export class SomeClass {
>SomeClass : SomeClass

@Input event: Event;
>Input : (target: any, key: string) => void
>event : Event
>Event : Event
}
14 changes: 14 additions & 0 deletions tests/cases/compiler/metadataOfEventAlias.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// @experimentalDecorators: true
// @emitDecoratorMetadata: true
// @target: es5
// @includeBuiltFile: lib.d.ts

// @filename: event.ts
export interface Event { title: string };

// @filename: test.ts
import { Event } from './event';
function Input(target: any, key: string): void { }
export class SomeClass {
@Input event: Event;
}