microsoft / microsoft/TypeScript

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

Offen
#29,578 0 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

@sandersn arbeitet bereits daran.

Seit 25.1.2019.

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

Beschreibung

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:

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.