Skip to content

Incorrect function type signature for ParameterDecorator #33260

@bajtos

Description

@bajtos

TypeScript Version: 3.7.0-dev.20190905

Search Terms: ParameterDecorator propertyKey

Code

The type ParameterDecorator is defined as follows in src/lib/es5.d.ts:

declare type ParameterDecorator = (target: Object, propertyKey: string | symbol, parameterIndex: number) => void;

However, at runtime, constructor parameter decorators are invoked with propertyKey set to undefined.

Consider the following code:

function inject() {
  return function i(target: Object,
    propertyKey: string | symbol,
    parameterIndex: number
  ): void {
    console.log('propertyKey', typeof propertyKey);
  }
}

class FooBar {
  constructor(
    @inject()
    public name: string
  ) {}
}

Compile it with experimentalDecorators enabled in tsconfig.json.

Expected behavior:

Either:

  • The code does not compile, because the decorator function must accept string | symbol | undefined in the propertyKey argument
  • Or the program prints propertyKey string or propertyKey symbol at runtime

Actual behavior:

  • The code compiles
  • The programs prints undefined at runtime

Playground Link:

http://www.typescriptlang.org/play/index.html?experimentalDecorators=true#code/GYVwdgxgLglg9mABDMArAptAFASkQbwChFEAndKEUpUSWBZLKAQ1IHMKAuRAeQCMM0ADTESiAA6k449KSgBPANLp53AM5RSKNogA+iNfIC2fOABsRYia2ZGKsgJJgAJugAe3MCBOzRObgBucDDOBKIkEAhq5ugAdGZwbFgA5JLSsgrK8slCiAoycMASUjJySio4ANyiAL6EdYQQZsxqaogAYnBwAEKsYRFRmiDQcKRY4YgAAiiCULgT4iB8ZjAQiGC26Oqa2n4EdXVAA

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions