microsoft / microsoft/TypeScript

Type variables in TypedPropertyDescriptor cause compilation errors when using decorator

Offen
#33,405 3 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@rbuckton arbeitet bereits daran.

Seit 17.9.2019.

Needs Investigation
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.