microsoft / microsoft/TypeScript

Decorated function parameter types are not inferred

Abierto
#54,652 2 comentarios 10 reacciones 1 asignado Ver en GitHub

@rbuckton ya está trabajando en esto.

Desde el 19/6/2023.

Needs Investigation
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.