microsoft / microsoft/TypeScript

When assigning a generic to a generic type prop, it is not evaluated like a set

Offen
#55,524 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

🔎 Search Terms

generic

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

https://codesandbox.io/s/ts-bugs-p9lrhh?file=/src/test.tsx

💻 Code
type Merge<T, U> = Omit<T, keyof U> & U;
type PropsWithAs<P, E extends React.ElementType> = P & { as?: E };
type PolymorphicProps<P, E extends React.ElementType> = Merge<
  React.ComponentPropsWithoutRef<E>,
  PropsWithAs<P, E>
>;
type Props = { color?: string };

function PolymorphicDiv<E extends React.ElementType>({ as }: { as?: E }) {
  const Tag = as ?? "div";
  return <Tag />;
}

function PolymorphicFlex<E extends React.ElementType = "div">({
  as,
  ...rest
}: PolymorphicProps<Props, E>) {
  return <PolymorphicDiv as={as} display="flex" {...rest} />;
}
function ExpectedBehavior<E extends React.ElementType = "div">({
  as,
  ...rest
}: PolymorphicProps<Props, E>) {
  return <PolymorphicFlex as={as} {...rest} />;
}

type PP<P, E extends React.ElementType> = Merge<
  PropsWithAs<P, E>,
  React.ComponentPropsWithoutRef<E>
>;

🙁 Actual behavior
// It works like `IntrinsicAttributes && Omit<PropsWithoutRef<ComponentProps<E>>, "as" | "color"> && Props && { as?: E }`
function ExpectedBehavior<E extends React.ElementType = "div">({
  as,
  ...rest
}: PolymorphicProps<Props, E>) {
  return <PolymorphicFlex as={as} {...rest} />;
}

// Error
// Type '{ as: E | undefined; } & Omit<PolymorphicProps<Props, E>, "as">' is not assignable to type 'IntrinsicAttributes & Omit<PropsWithoutRef<ComponentProps<E>>, "as" | "color"> & Props & { ...; }'.
// Type '{ as: E | undefined; } & Omit<PolymorphicProps<Props, E>, "as">' is not assignable to type 'Omit<PropsWithoutRef<ComponentProps<E>>, "as" | "color">'.

It seems to be evaluated like a && conditional statement which type can be assigned to a type.
However, if a type can be assigned, it must be evaluated like a set. (like a || conditional statement)
(When as is not assignable to Omit<PropsWithoutRef<ComponentProps<E>>, "as" | "color">, it returns false immediately but actually it is assignable to PolymorphicProps type.)

🙂 Expected behavior
// When reverse merge order, it works
type PP<P, E extends React.ElementType> = Merge<
  PropsWithAs<P, E>,
  React.ComponentPropsWithoutRef<E>
>;

function ExpectedBehavior<E extends React.ElementType = "div">({
  as,
  ...rest
}: PP<Props, E>) {
  return <PolymorphicFlex as={as} {...rest} />;
}

And I think it is bug, because when I reverse merge order (then TypeScript evaluate as is assignable to Omit<Props & { as?: E }, "color" | ...> & IntrinsicAttributes & PropsWithoutRef<ComponentProps<E>>) it works.

Additional information about the issue

No response

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

Beginne mit der verlinkten CodeSandbox und reproduziere das generische React-Beispiel, wobei du die beiden im Issue gezeigten Merge-Reihenfolgen vergleichst. Verfolge den beteiligten TypeScript-Typprüfpfad; abgeschlossen ist die Aufgabe, wenn ein Regressionstest die fehlschlagende Reihenfolge abdeckt und sich die gemeldete Zuweisung wie erwartet verhält.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
react, typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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