Skip to content

Commit 652a1c5

Browse files
authored
Emit fallback for decorator metadata for type only imports (microsoft#39337)
1 parent a60feb2 commit 652a1c5

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
@@ -36989,29 +36989,31 @@ namespace ts {
3698936989
}
3699036990

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

3699436996
// Resolve the symbol as a type so that we can provide a more useful hint for the type serializer.
3699536997
const typeSymbol = resolveEntityName(typeName, SymbolFlags.Type, /*ignoreErrors*/ true, /*dontResolveAlias*/ false, location);
36996-
if (valueSymbol && valueSymbol === typeSymbol) {
36998+
if (resolvedSymbol && resolvedSymbol === typeSymbol) {
3699736999
const globalPromiseSymbol = getGlobalPromiseConstructorSymbol(/*reportErrors*/ false);
36998-
if (globalPromiseSymbol && valueSymbol === globalPromiseSymbol) {
37000+
if (globalPromiseSymbol && resolvedSymbol === globalPromiseSymbol) {
3699937001
return TypeReferenceSerializationKind.Promise;
3700037002
}
3700137003

37002-
const constructorType = getTypeOfSymbol(valueSymbol);
37004+
const constructorType = getTypeOfSymbol(resolvedSymbol);
3700337005
if (constructorType && isConstructorType(constructorType)) {
37004-
return TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
37006+
return isTypeOnly ? TypeReferenceSerializationKind.TypeWithCallSignature : TypeReferenceSerializationKind.TypeWithConstructSignatureAndValue;
3700537007
}
3700637008
}
3700737009

3700837010
// We might not be able to resolve type symbol so use unknown type in that case (eg error case)
3700937011
if (!typeSymbol) {
37010-
return TypeReferenceSerializationKind.Unknown;
37012+
return isTypeOnly ? TypeReferenceSerializationKind.ObjectType : TypeReferenceSerializationKind.Unknown;
3701137013
}
3701237014
const type = getDeclaredTypeOfSymbol(typeSymbol);
3701337015
if (type === errorType) {
37014-
return TypeReferenceSerializationKind.Unknown;
37016+
return isTypeOnly ? TypeReferenceSerializationKind.ObjectType : TypeReferenceSerializationKind.Unknown;
3701537017
}
3701637018
else if (type.flags & TypeFlags.AnyOrUnknown) {
3701737019
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)