microsoft / microsoft/TypeScript

Generic should expand in union

Aperta
#43,585 4 commenti 2 reazioni 1 assegnatario Vedi su GitHub

@weswigham ci sta già lavorando.

Dal 9/4/2021.

Needs Investigation Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Bug Report

🔎 Search Terms

generic expansion, union expansion

🕗 Version & Regression Information
  • This changed between versions 4.1.5 and 4.2.3 (and the new behavior remains in 4.3.0-dev.20210407)
⏯ Playground Link
💻 Code
type Maybe<T> =
    | null
    | T

const isResolved = <T>(maybe: Maybe<T>): maybe is T =>
    maybe !== null;

const map = <T, U>(
    maybe: Maybe<T>,
    mapFn: (value: T) => U,
): Maybe<U> => isResolved(maybe) ? mapFn(maybe) : maybe;

// Using an array in this example because it makes the failure obvious when spreading it,
// but the problematic behavior is not specific to arrays.
declare const foo: Maybe<Maybe<string[]>>;
map(foo, t => [...t]); // t is string[] in 4.1, Maybe<string[]> (and error 2488) in 4.2.
map(foo, (t: string[]) => t); // t is string[] in both 4.1 and 4.2 with the annotation.
🙁 Actual behavior

t in map(foo, t => [...t]); is a Maybe<string[]>.

It seems like when T in Maybe is another Maybe, it's opaque (not expanded).

🙂 Expected behavior

t in map(foo, t => [...t]); is a string[].

It seems like when T in Maybe is another Maybe, it's expanded. For example Maybe<Maybe<string[]>> is expanded to null | (null | string []), then collapsed to null | string[], which is the same as Maybe<string[]>.

It seems like this is the desired behavior, because at runtime, t will indeed only ever be string[].

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.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.