Skip to content

bug: Decorator Method Name Type Restriction By Enum #30667

@ozyman42

Description

@ozyman42

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.

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions