microsoft / microsoft/TypeScript

Error when initialising variable with generic parameter type

Offen
#53,620 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bug Domain: Mapped Types Help Wanted
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Bug Report

I am seeing an error when trying to initialise a variable with

const result: Partial<T> & { bar: boolean } = { bar: false };

where T is a type parameter in a generic function. The error is as follows:

4:9 - error TS2322: Type '{ bar: false; }' is not assignable to type 'Partial<T> & { bar: boolean; }'.
  Type '{ bar: false; }' is not assignable to type 'Partial<T>'.

4   const result: Partial<T> & { bar: boolean } = { bar: false };
          ~~~~~~

If T is a type that is not a generic parameter then there are no issues, and also adding an additional step as follows leads to no errors:

  const part: Partial<T> = {};
  const result: Partial<T> & { bar: boolean } = { ...part, bar: false };

I have included a full code snippet below.

🔎 Search Terms
  • "is not assignable partial"
  • "intersection type generic function error"
🕗 Version & Regression Information
  • This is the behaviour in every version I tried, and I reviewed the FAQ for entries about 'Common "Bugs" That Aren't Bugs'.
⏯ Playground Link

Playground link with relevant code

💻 Code
type PartialWithBar<T> = Partial<T> & { bar: boolean };

export const erroringFunc = <T extends Record<string, unknown>>(): PartialWithBar<T> => {
  const result: PartialWithBar<T> = { bar: false };
  return result;
};

export const workingFunc = <T extends Record<string, unknown>>(): PartialWithBar<T> => {
  const part: Partial<T> = {};
  const result: PartialWithBar<T> = { ...part, bar: false };
  return result;
};

type MyType = { a: number };

export const workingFunc2 = (): PartialWithBar<MyType> => {
  const result: PartialWithBar<MyType> = { bar: false };
  return result;
};
🙁 Actual behavior

erroringFunc causes an error, while workingFunc does not.

🙂 Expected behavior

Either errorringFunc should not cause any errors, or workingFunc should cause an error - the two should be consistent.

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.

Rechercherichtung

Beginnen Sie mit dem verlinkten TypeScript Playground und vergleichen Sie das Type-Checking-Verhalten von erroringFunc, workingFunc und workingFunc2 im bereitgestellten Snippet. Untersuchen Sie, warum das Objektliteral für PartialWithBar abgelehnt wird, während die Spread-Form akzeptiert wird; abgeschlossen ist die Aufgabe, wenn die beiden generischen Formen ein konsistentes, getestetes Verhalten zeigen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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