Skip to content
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

Incorrect function type signature for ParameterDecorator #33260

Closed
bajtos opened this issue Sep 5, 2019 · 2 comments
Closed

Incorrect function type signature for ParameterDecorator #33260

bajtos opened this issue Sep 5, 2019 · 2 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript
Milestone

Comments

@bajtos
Copy link

bajtos commented Sep 5, 2019

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:

@bajtos
Copy link
Author

bajtos commented Sep 5, 2019

I find the current behavior problematic because the type definition for ParameterDecorator are not matching the actual runtime behavior.

I'd like to propose two different solutions for your consideration:

  1. Change the ParameterDecorator type - add undefined to allowed propertyKey values.

    declare type ParameterDecorator = (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) => void; 
  2. Modify the runtime to set propertyKey to an empty string '' instead of undefined when calling parameter decorators for constructor arguments.

Personally, I prefer the first one. I'd rather have my existing codebase to fail the compilation rather than discover a subtle change in TS runtime in production.

@jakebailey
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants