microsoft / microsoft/TypeScript
bug: Decorator Method Name Type Restriction By Enum
@rbuckton is already working on this.
Since Apr 1, 2019.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.3.3
Search Terms:
is:issue decorator type method name propertyKey
Code:
this works as expected:
interface SomeTypeMap {
fieldOne: string;
fieldTwo: number;
}
function MethodDecorator<Key extends keyof SomeTypeMap>(
target: SomeClass,
methodName: Key,
descriptor: TypedPopertyDescriptor<(...args: any[]) => SomeTypeMap[Key]>) {
/* some implementation */
}
class SomeClass {
// works fine, as expected
@MethodDecorator
public fieldOne() {
return "";
}
// compiler error, as expected
// since it returns string instead of number
@MethodDecorator
public fieldTwo() {
return "";
}
// compiler error, as expected
// since method name is not a key in SomeTypeMap
@MethodDecorator
public fieldThree() {
return "";
}
}
this does not work as expected:
enum Field {
One = "fieldOne"
}
interface SomeTypeMap {
[Field.One]: string;
}
function MethodDecorator<Key extends keyof SomeTypeMap>(
target: SomeClass,
methodName: Key,
descriptor: TypedPopertyDescriptor<(...args: any[]) => SomeTypeMap[Key]>) {
/* some implementation */
}
class SomeClass {
// compiler error, unexpected
@MethodDecorator
public [Field.One]() {
return "";
}
}
Expected behavior:
In the second code example, I would expect there to be no compiler errors.
Actual behavior:
Error message: Argument of type 'string' is not assignable to parameter of type Field
The method name seems to be of type string here, which is true, but it should also be of type Field
Playground Link:
can't enable experimental decorators in the playground.
Related Issues:
#17795
less related:
#30102
Comments:
Thanks for all your hard work TypeScript team.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.