Skip to content

Commit 1809250

Browse files
committed
Emit fallback for decorator metadata for type only imports
1 parent 9458f8a commit 1809250

File tree

5 files changed

+149
-7
lines changed

5 files changed

+149
-7
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36973,29 +36973,31 @@ namespace ts {
3697336973
}
3697436974

3697536975
// Resolve the symbol as a value to ensure the type can be reached at runtime during emit.
36976-
const valueSymbol = resolveEntityName(typeName, SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
36976+
const valueSymbol = resolveEntityName(typeName, SymbolFlags.Value, /*ignoreErrors*/ true, /*dontResolveAlias*/ true, location);
36977+
const isTypeOnly = valueSymbol?.declarations?.every(isTypeOnlyImportOrExportDeclaration) || false;
36978+
const resolvedSymbol = valueSymbol && valueSymbol.flags & SymbolFlags.Alias ? resolveAlias(valueSymbol) : valueSymbol;
3697736979

3697836980
// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
3697936981
const typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
36980-
if (valueSymbol && valueSymbol === typeSymbol) {
36982+
if (resolvedSymbol && resolvedSymbol === typeSymbol) {
3698136983
const globalPromiseSymbol = getGlobalPromiseConstructorSymbol(/*reportErrors*/ false);
36982-
if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) {
36984+
if (globalPromiseSymbol && resolvedSymbol === globalPromiseSymbol) {
3698336985
return TypeReferenceSerializationKind.Promise;
3698436986
}
3698536987

36986-
const constructorType = getTypeOfSymbol(valueSymbol);
36988+
const constructorType = getTypeOfSymbol(resolvedSymbol);
3698736989
if (constructorType && isConstructorType(constructorType)) {
36988-
return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
36990+
return isTypeOnly ? TypeReferenceSerializationKind.TypeWithCallSignature : TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
3698936991
}
3699036992
}
3699136993

3699236994
// We might not be able to resolve type symbol so use unknown type in that case (eg error case)
3699336995
if (!typeSymbol) {
36994-
return TypeReferenceSerializationKind.Unknown;
36996+
return isTypeOnly ? TypeReferenceSerializationKind.ObjectType : TypeReferenceSerializationKind.Unknown;
3699536997
}
3699636998
const type = getDeclaredTypeOfSymbol(typeSymbol);
3699736999
if (type === errorType) {
36998-
return TypeReferenceSerializationKind.Unknown;
37000+
return isTypeOnly ? TypeReferenceSerializationKind.ObjectType : TypeReferenceSerializationKind.Unknown;
3699937001
}
3700037002
else if (type.flags & TypeFlags.AnyOrUnknown) {
3700137003
return TypeReferenceSerializationKind.ObjectType;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//// [tests/cases/conformance/decorators/decoratorMetadataWithTypeOnlyImport.ts] ////
2+
3+
//// [service.ts]
4+
export class Service {
5+
}
6+
//// [component.ts]
7+
import type { Service } from "./service";
8+
9+
declare var decorator: any;
10+
11+
@decorator
12+
class MyComponent {
13+
constructor(public Service: Service) {
14+
}
15+
16+
@decorator
17+
method(x: this) {
18+
}
19+
}
20+
21+
//// [service.js]
22+
"use strict";
23+
Object.defineProperty(exports, "__esModule", { value: true });
24+
exports.Service = void 0;
25+
var Service = /** @class */ (function () {
26+
function Service() {
27+
}
28+
return Service;
29+
}());
30+
exports.Service = Service;
31+
//// [component.js]
32+
"use strict";
33+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
34+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
35+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
36+
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;
37+
return c > 3 && r && Object.defineProperty(target, key, r), r;
38+
};
39+
var __metadata = (this && this.__metadata) || function (k, v) {
40+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
41+
};
42+
Object.defineProperty(exports, "__esModule", { value: true });
43+
var MyComponent = /** @class */ (function () {
44+
function MyComponent(Service) {
45+
this.Service = Service;
46+
}
47+
MyComponent.prototype.method = function (x) {
48+
};
49+
__decorate([
50+
decorator,
51+
__metadata("design:type", Function),
52+
__metadata("design:paramtypes", [Object]),
53+
__metadata("design:returntype", void 0)
54+
], MyComponent.prototype, "method", null);
55+
MyComponent = __decorate([
56+
decorator,
57+
__metadata("design:paramtypes", [Function])
58+
], MyComponent);
59+
return MyComponent;
60+
}());
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/conformance/decorators/service.ts ===
2+
export class Service {
3+
>Service : Symbol(Service, Decl(service.ts, 0, 0))
4+
}
5+
=== tests/cases/conformance/decorators/component.ts ===
6+
import type { Service } from "./service";
7+
>Service : Symbol(Service, Decl(component.ts, 0, 13))
8+
9+
declare var decorator: any;
10+
>decorator : Symbol(decorator, Decl(component.ts, 2, 11))
11+
12+
@decorator
13+
>decorator : Symbol(decorator, Decl(component.ts, 2, 11))
14+
15+
class MyComponent {
16+
>MyComponent : Symbol(MyComponent, Decl(component.ts, 2, 27))
17+
18+
constructor(public Service: Service) {
19+
>Service : Symbol(MyComponent.Service, Decl(component.ts, 6, 16))
20+
>Service : Symbol(Service, Decl(component.ts, 0, 13))
21+
}
22+
23+
@decorator
24+
>decorator : Symbol(decorator, Decl(component.ts, 2, 11))
25+
26+
method(x: this) {
27+
>method : Symbol(MyComponent.method, Decl(component.ts, 7, 5))
28+
>x : Symbol(x, Decl(component.ts, 10, 11))
29+
}
30+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/conformance/decorators/service.ts ===
2+
export class Service {
3+
>Service : Service
4+
}
5+
=== tests/cases/conformance/decorators/component.ts ===
6+
import type { Service } from "./service";
7+
>Service : Service
8+
9+
declare var decorator: any;
10+
>decorator : any
11+
12+
@decorator
13+
>decorator : any
14+
15+
class MyComponent {
16+
>MyComponent : MyComponent
17+
18+
constructor(public Service: Service) {
19+
>Service : Service
20+
}
21+
22+
@decorator
23+
>decorator : any
24+
25+
method(x: this) {
26+
>method : (x: this) => void
27+
>x : this
28+
}
29+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// @experimentalDecorators: true
2+
// @emitDecoratorMetadata: true
3+
// @target: es5
4+
// @module: commonjs
5+
// @filename: service.ts
6+
export class Service {
7+
}
8+
// @filename: component.ts
9+
import type { Service } from "./service";
10+
11+
declare var decorator: any;
12+
13+
@decorator
14+
class MyComponent {
15+
constructor(public Service: Service) {
16+
}
17+
18+
@decorator
19+
method(x: this) {
20+
}
21+
}

0 commit comments

Comments
 (0)