microsoft / microsoft/TypeScript

unclear how to make function argument check produce the same errors as variable assignment

Abierto
#29,578 0 comentarios 0 reacciones 1 asignado Ver en GitHub

@sandersn ya está trabajando en esto.

Desde el 25/1/2019.

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

Descripción

in the following code

type A = {
  a?: number
  b?: number
}

type B = {
  a?: number
  knownField?: number
}

type C = {
  a?: A,
  knownField?: number | [A, B] | A
}

const c: C = {
  a: {
    a: 1,
    b: 1,
  },
  knownField: [{ a: 1 }, {
    knownField: 1,
    unknownField: 1,
  }],
}

const fn = (c: C) => {}

fn({
  a: {
    a: 1,
    b: 1,
  },
  knownField: [{ a: 1 }, {
    knownField: 1,
    unknownField: 1,
  }],
})
  • in the case of variable assignment it correctly highlights the unknownField and produces a nice error that says Object literal may only specify known properties, and 'unknownField' does not exist in type 'B'.
  • but in the case of the same value passed as a function argument (of the same type) it highlights the top-level field and a different, less readable error Type '({ a: number; } | { b: number; unknownField: number; })[]' is missing the following properties from type '[A, B]': 0, 1

am I missing something? is there a way to make sure I get the same error and highlighting in both cases?

in typescript playground:

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.