microsoft / microsoft/TypeScript

TypeScript incorrectly says return is not assignable to an intersection containing all the required props

Aperta
#43,366 4 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: Intersection
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Bug Report

🔎 Search Terms

& generic intersection omit spread

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics
⏯ Playground Link

Playground link with relevant code

💻 Code
export interface BaseProps {
  id?: string;
  name: string;
}

export type PropsWithId<P extends BaseProps> = Omit<
  P,
  'id'
> & { id: string };

export default function usePropsWithId<P extends BaseProps>(
  props: P
): PropsWithId<P> {
  const { id: idProp, name, ...other } = props;
  const id = idProp ?? `${name}-id`;

  return { id, name, ...other };
}
🙁 Actual behavior

The return line gets this TypeScript error:

Type '{ id: string; name: string; } & Omit<P, "id" | "name">' is not assignable to type 'PropsWithId<P>'.
  Type '{ id: string; name: string; } & Omit<P, "id" | "name">' is not assignable to type 'Omit<P, "id">'.

This doesn't make sense since PropsWithId<P> is Omit<P, 'id'> & { id: string };

🙂 Expected behavior

This should work without a TypeScript error.

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 con l’esempio di intersezione generica nell’issue, quindi traccia il modo in cui il checker valuta l’oggetto restituito rispetto a PropsWithId

. L’issue non indica file o test del repository; il lavoro è completo quando l’esempio viene compilato senza l’errore di assegnabilità segnalato, mantenendo il tipo PropsWithId

previsto.

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
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.