Skip to content

Improve parameter decorator inference #47591

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

Closed
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
10 changes: 5 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29789,11 +29789,11 @@ namespace ts {
case SyntaxKind.Parameter:
// A parameter declaration decorator will have three arguments (see
// `ParameterDecorator` in core.d.ts).
const func = parent.parent as FunctionLikeDeclaration;
const element = parent.parent as MethodDeclaration | ConstructorDeclaration;
return [
createSyntheticExpression(expr, parent.parent.kind === SyntaxKind.Constructor ? getTypeOfSymbol(getSymbolOfNode(func)) : errorType),
createSyntheticExpression(expr, anyType),
createSyntheticExpression(expr, numberType)
createSyntheticExpression(expr, getParentTypeOfClassElement(element)),
createSyntheticExpression(expr, element.kind === SyntaxKind.Constructor ? undefinedType : getClassElementPropertyKeyType(element)),
createSyntheticExpression(expr, getNumberLiteralType(element.parameters.indexOf(parent as ParameterDeclaration)))
];
case SyntaxKind.PropertyDeclaration:
case SyntaxKind.MethodDeclaration:
Expand Down Expand Up @@ -41441,7 +41441,7 @@ namespace ts {
*/
function getParentTypeOfClassElement(node: ClassElement) {
const classSymbol = getSymbolOfNode(node.parent)!;
return isStatic(node)
return isStatic(node) || node.kind === SyntaxKind.Constructor
? getTypeOfSymbol(classSymbol)
: getDeclaredTypeOfSymbol(classSymbol);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ interface TypedPropertyDescriptor<T> {
declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;
declare type ParameterDecorator = (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) => void;

declare type PromiseConstructorLike = new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>;

Expand Down
15 changes: 15 additions & 0 deletions tests/baselines/reference/parameterDecoratorIndex1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
tests/cases/compiler/parameterDecoratorIndex1.ts(6,4): error TS2345: Argument of type '1' is not assignable to parameter of type '0'.


==== tests/cases/compiler/parameterDecoratorIndex1.ts (1 errors) ====
const decorator = (target: any, key: string, index: 0) => {};

class A {
method(
@decorator param1: string,
@decorator param2: string
~~~~~~~~~
!!! error TS2345: Argument of type '1' is not assignable to parameter of type '0'.
) {}
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/parameterDecoratorIndex1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [parameterDecoratorIndex1.ts]
const decorator = (target: any, key: string, index: 0) => {};

class A {
method(
@decorator param1: string,
@decorator param2: string
) {}
}


//// [parameterDecoratorIndex1.js]
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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var decorator = function (target, key, index) { };
var A = /** @class */ (function () {
function A() {
}
A.prototype.method = function (param1, param2) { };
__decorate([
__param(0, decorator),
__param(1, decorator)
], A.prototype, "method");
return A;
}());
24 changes: 24 additions & 0 deletions tests/baselines/reference/parameterDecoratorIndex1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/parameterDecoratorIndex1.ts ===
const decorator = (target: any, key: string, index: 0) => {};
>decorator : Symbol(decorator, Decl(parameterDecoratorIndex1.ts, 0, 5))
>target : Symbol(target, Decl(parameterDecoratorIndex1.ts, 0, 19))
>key : Symbol(key, Decl(parameterDecoratorIndex1.ts, 0, 31))
>index : Symbol(index, Decl(parameterDecoratorIndex1.ts, 0, 44))

class A {
>A : Symbol(A, Decl(parameterDecoratorIndex1.ts, 0, 61))

method(
>method : Symbol(A.method, Decl(parameterDecoratorIndex1.ts, 2, 9))

@decorator param1: string,
>decorator : Symbol(decorator, Decl(parameterDecoratorIndex1.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorIndex1.ts, 3, 8))

@decorator param2: string
>decorator : Symbol(decorator, Decl(parameterDecoratorIndex1.ts, 0, 5))
>param2 : Symbol(param2, Decl(parameterDecoratorIndex1.ts, 4, 28))

) {}
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/parameterDecoratorIndex1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/parameterDecoratorIndex1.ts ===
const decorator = (target: any, key: string, index: 0) => {};
>decorator : (target: any, key: string, index: 0) => void
>(target: any, key: string, index: 0) => {} : (target: any, key: string, index: 0) => void
>target : any
>key : string
>index : 0

class A {
>A : A

method(
>method : (param1: string, param2: string) => void

@decorator param1: string,
>decorator : (target: any, key: string, index: 0) => void
>param1 : string

@decorator param2: string
>decorator : (target: any, key: string, index: 0) => void
>param2 : string

) {}
}

13 changes: 13 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey1.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/parameterDecoratorKey1.ts(5,7): error TS2345: Argument of type '"bar"' is not assignable to parameter of type '"foo"'.


==== tests/cases/compiler/parameterDecoratorKey1.ts (1 errors) ====
const decorator = (target: any, key: "foo", index: number) => {};

class A {
foo(@decorator param1: string) {}
bar(@decorator param1: string) {}
~~~~~~~~~
!!! error TS2345: Argument of type '"bar"' is not assignable to parameter of type '"foo"'.
}

33 changes: 33 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [parameterDecoratorKey1.ts]
const decorator = (target: any, key: "foo", index: number) => {};

class A {
foo(@decorator param1: string) {}
bar(@decorator param1: string) {}
}


//// [parameterDecoratorKey1.js]
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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var decorator = function (target, key, index) { };
var A = /** @class */ (function () {
function A() {
}
A.prototype.foo = function (param1) { };
A.prototype.bar = function (param1) { };
__decorate([
__param(0, decorator)
], A.prototype, "foo");
__decorate([
__param(0, decorator)
], A.prototype, "bar");
return A;
}());
21 changes: 21 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey1.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/parameterDecoratorKey1.ts ===
const decorator = (target: any, key: "foo", index: number) => {};
>decorator : Symbol(decorator, Decl(parameterDecoratorKey1.ts, 0, 5))
>target : Symbol(target, Decl(parameterDecoratorKey1.ts, 0, 19))
>key : Symbol(key, Decl(parameterDecoratorKey1.ts, 0, 31))
>index : Symbol(index, Decl(parameterDecoratorKey1.ts, 0, 43))

class A {
>A : Symbol(A, Decl(parameterDecoratorKey1.ts, 0, 65))

foo(@decorator param1: string) {}
>foo : Symbol(A.foo, Decl(parameterDecoratorKey1.ts, 2, 9))
>decorator : Symbol(decorator, Decl(parameterDecoratorKey1.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorKey1.ts, 3, 5))

bar(@decorator param1: string) {}
>bar : Symbol(A.bar, Decl(parameterDecoratorKey1.ts, 3, 34))
>decorator : Symbol(decorator, Decl(parameterDecoratorKey1.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorKey1.ts, 4, 5))
}

22 changes: 22 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey1.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/parameterDecoratorKey1.ts ===
const decorator = (target: any, key: "foo", index: number) => {};
>decorator : (target: any, key: "foo", index: number) => void
>(target: any, key: "foo", index: number) => {} : (target: any, key: "foo", index: number) => void
>target : any
>key : "foo"
>index : number

class A {
>A : A

foo(@decorator param1: string) {}
>foo : (param1: string) => void
>decorator : (target: any, key: "foo", index: number) => void
>param1 : string

bar(@decorator param1: string) {}
>bar : (param1: string) => void
>decorator : (target: any, key: "foo", index: number) => void
>param1 : string
}

13 changes: 13 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey2.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/parameterDecoratorKey2.ts(4,15): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'string'.


==== tests/cases/compiler/parameterDecoratorKey2.ts (1 errors) ====
const decorator = (target: any, key: string, index: number) => {};

class A {
constructor(@decorator param1: string) {}
~~~~~~~~~
!!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'string'.
method(@decorator param1: string) {}
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [parameterDecoratorKey2.ts]
const decorator = (target: any, key: string, index: number) => {};

class A {
constructor(@decorator param1: string) {}
method(@decorator param1: string) {}
}


//// [parameterDecoratorKey2.js]
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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var decorator = function (target, key, index) { };
var A = /** @class */ (function () {
function A(param1) {
}
A.prototype.method = function (param1) { };
__decorate([
__param(0, decorator)
], A.prototype, "method");
A = __decorate([
__param(0, decorator)
], A);
return A;
}());
20 changes: 20 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/parameterDecoratorKey2.ts ===
const decorator = (target: any, key: string, index: number) => {};
>decorator : Symbol(decorator, Decl(parameterDecoratorKey2.ts, 0, 5))
>target : Symbol(target, Decl(parameterDecoratorKey2.ts, 0, 19))
>key : Symbol(key, Decl(parameterDecoratorKey2.ts, 0, 31))
>index : Symbol(index, Decl(parameterDecoratorKey2.ts, 0, 44))

class A {
>A : Symbol(A, Decl(parameterDecoratorKey2.ts, 0, 66))

constructor(@decorator param1: string) {}
>decorator : Symbol(decorator, Decl(parameterDecoratorKey2.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorKey2.ts, 3, 13))

method(@decorator param1: string) {}
>method : Symbol(A.method, Decl(parameterDecoratorKey2.ts, 3, 42))
>decorator : Symbol(decorator, Decl(parameterDecoratorKey2.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorKey2.ts, 4, 8))
}

21 changes: 21 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/parameterDecoratorKey2.ts ===
const decorator = (target: any, key: string, index: number) => {};
>decorator : (target: any, key: string, index: number) => void
>(target: any, key: string, index: number) => {} : (target: any, key: string, index: number) => void
>target : any
>key : string
>index : number

class A {
>A : A

constructor(@decorator param1: string) {}
>decorator : (target: any, key: string, index: number) => void
>param1 : string

method(@decorator param1: string) {}
>method : (param1: string) => void
>decorator : (target: any, key: string, index: number) => void
>param1 : string
}

13 changes: 13 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey3.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/parameterDecoratorKey3.ts(5,10): error TS2345: Argument of type '"method"' is not assignable to parameter of type 'undefined'.


==== tests/cases/compiler/parameterDecoratorKey3.ts (1 errors) ====
const decorator = (target: any, key: undefined, index: number) => {};

class A {
constructor(@decorator param1: string) {}
method(@decorator param1: string) {}
~~~~~~~~~
!!! error TS2345: Argument of type '"method"' is not assignable to parameter of type 'undefined'.
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//// [parameterDecoratorKey3.ts]
const decorator = (target: any, key: undefined, index: number) => {};

class A {
constructor(@decorator param1: string) {}
method(@decorator param1: string) {}
}


//// [parameterDecoratorKey3.js]
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 __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var decorator = function (target, key, index) { };
var A = /** @class */ (function () {
function A(param1) {
}
A.prototype.method = function (param1) { };
__decorate([
__param(0, decorator)
], A.prototype, "method");
A = __decorate([
__param(0, decorator)
], A);
return A;
}());
20 changes: 20 additions & 0 deletions tests/baselines/reference/parameterDecoratorKey3.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/parameterDecoratorKey3.ts ===
const decorator = (target: any, key: undefined, index: number) => {};
>decorator : Symbol(decorator, Decl(parameterDecoratorKey3.ts, 0, 5))
>target : Symbol(target, Decl(parameterDecoratorKey3.ts, 0, 19))
>key : Symbol(key, Decl(parameterDecoratorKey3.ts, 0, 31))
>index : Symbol(index, Decl(parameterDecoratorKey3.ts, 0, 47))

class A {
>A : Symbol(A, Decl(parameterDecoratorKey3.ts, 0, 69))

constructor(@decorator param1: string) {}
>decorator : Symbol(decorator, Decl(parameterDecoratorKey3.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorKey3.ts, 3, 13))

method(@decorator param1: string) {}
>method : Symbol(A.method, Decl(parameterDecoratorKey3.ts, 3, 42))
>decorator : Symbol(decorator, Decl(parameterDecoratorKey3.ts, 0, 5))
>param1 : Symbol(param1, Decl(parameterDecoratorKey3.ts, 4, 8))
}

Loading