microsoft / microsoft/TypeScript

Type variables in TypedPropertyDescriptor cause compilation errors when using decorator

Open
#33,405 3 comments 0 reactions 1 assignee View on GitHub

@rbuckton is already working on this.

Since Sep 17, 2019.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.6.3

Search Terms: TypedPropertyDescriptor, MethodDecorator, TS2345, Types of property 'value' are incompatible.

Code

type MyDynamicDescriptor = <T>(input: T) => T
type MyStaticDescriptor = (input: number) => number

function dynamicDecorator(foo: string) {
  return (_: any, __: any, descriptor: TypedPropertyDescriptor<MyDynamicDescriptor>) => {
    console.log('foo:', foo)
    console.log('descriptor.value:', descriptor.value)
  }
}

function staticDecorator(foo: string) {
  return (_: any, __: any, descriptor: TypedPropertyDescriptor<MyStaticDescriptor>) => {
    console.log('foo:', foo)
    console.log('descriptor.value:', descriptor.value)
  }
}

class myClass {
  @dynamicDecorator('bar')
  myMethod(input: number): number {
    return input + 42
  }

  @staticDecorator('bar')
  myOtherMethod(input: number): number {
    return input + 42
  }
}

Expected behavior:
No compilation errors for both dynamicDecorator and staticDecorator.

Actual behavior:
Compilation errors for dynamicDecorator:

error TS2345: Argument of type 'TypedPropertyDescriptor<(input: number) => Promise<number>>' is not assignable to parameter of type 'TypedPropertyDescriptor<MyDynamicDescriptor>'.
  Types of property 'value' are incompatible.
    Type '((input: number) => Promise<number>) | undefined' is not assignable to type 'MyDynamicDescriptor | undefined'.
      Type '(input: number) => Promise<number>' is not assignable to type 'MyDynamicDescriptor'.

Playground Link

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.