microsoft / microsoft/TypeScript

Error when initialising variable with generic parameter type

Aperta
#53,620 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Mapped Types Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con il TypeScript Playground collegato e confronta il comportamento del type-checking di erroringFunc, workingFunc e workingFunc2 nello snippet fornito. Analizza perché il literal dell’oggetto viene rifiutato per PartialWithBar, mentre la forma con spread viene accettata; il lavoro è completato quando le due forme generiche hanno un comportamento coerente e testato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Specificata chiaramente
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.