microsoft / microsoft/TypeScript

Decorated function parameter types are not inferred

Ouverte
#54,652 2 commentaires 10 réactions 1 personne assignée Voir sur GitHub

@rbuckton y travaille déjà.

Depuis le 19/6/2023.

Needs Investigation
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

Bug Report

When decorating a class method, the argument types are not inferred from the decorator. When using the decorator method directly, without decorator syntax, the types are inferred correctly.

🔎 Search Terms

decorator, infer, type, argument

🕗 Version & Regression Information
  • This happens for decorators introduced in 5.0
⏯ Playground Link

Playground link with relevant code

💻 Code
function decorate(f: (id: number) => void, context?: any): (id: number) => void { return f }

class Foo {
    @decorate
    one(id) { // Parameter 'id' implicitly has an 'any' type.(7006)
        console.log(id as string)
    }

    two = decorate(function two(id) {
        console.log(id as string) // Conversion of type 'number' to type 'string' may be a mistake… (2352)
    })
}

let foo = new Foo();
foo.one("foo")
foo.two("foo") // Argument of type 'string' is not assignable to parameter of type 'number'.(2345)
🙁 Actual behavior

The parameter id for method one is inferred as any

🙂 Expected behavior

The parameter is inferred as number, similar to how it works when calling the decorated function directly for method two.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.