Microsoft/TypeScript

Type modifier in mapped types break `any`

Open

#44.475 geöffnet am 7. Juni 2021

Auf GitHub ansehen
 (0 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)TypeScript (6.726 Forks)batch import
BugDomain: Mapped TypesHelp Wanted

Repository-Metriken

Stars
 (48.455 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 6T 17h) (9 gemergte PRs in 30 T)

Beschreibung

Bug Report

🔎 Search Terms

mapped types, any, contravariance, type modifiers

🕗 Version & Regression Information

4.3.2

⏯ Playground Link

(the playground seems down for me, but I'll update the ticket when I can)

💻 Code

    // Adding/removing `-?` makes the `anyCallback` line flip between broken/working
    type Mapped<T> = { [K in keyof T]-?: number };
    type Callback<T> = (object: Mapped<T>) => void;
    const fn: Callback<{ firstName: string }> = () => {};
    const anyCallback: Callback<any> = fn;

🙁 Actual behavior

The anyCallback = fn line causes an error:

TS2322: Type 'Callback<{ firstName: string; }>' is not assignable to type 'Callback<any>'.
  Types of parameters 'object' and 'object' are incompatible.
    Property 'firstName' is missing in type 'Mapped<any>' but required in type 'Mapped<{ firstName: string; }>'.

🙂 Expected behavior

I expect to be able to assign Callback<{ firstName: string} to Callback<any> because { firstName: string } should be considered any.

If I remove the -? type modifier in the Mapped type, then this works as expected.

Which leads me to the naive assertion that "the type modifier breaks any".

Note it's only the required type modifier -?, the required modifier +? doesn't cause the compile error.

Contributor Guide