microsoft / microsoft/TypeScript

Remove Map<any, any> constructor overload

Offen
#60,051 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

⚙ Compilation target

ES2017

⚙ Library

lib.es2015.iterable.d.ts

Missing / Incorrect Definition

This isn't exactly the kind of issue this form (Library issue) seems to be made for, but it looked like the closest match.

The Map constructor has an explicit overload to make new Map() produce a Map<any, any>, rather than safer option of allowing normal type inference to occur. I think the only change that needs to happen is removing the overload. This was already brought up in https://github.com/microsoft/TypeScript/issues/52552, but that was closed because it did not provide a clear usecase, and the focus was on inconsistency between Map and Set.

For me, the usecase is about Map alone - removing a source of silent and infectious anys. noImplicitAny is a recommended setting for good reason, but is undermined by the presence of anys in library types. In fact, I am creating this issue after fixing a bug in my own code that was hidden by this typing.

A fair counterargument is that it may break existing code. My guess - total speculation - is that the override was added in the past when inference was not as effective, and it is no longer necessary in most cases. Where the empty map is meant to conform to a contextual type, it works fine. Toying around with it myself, I find two main cases where it breaks:

  1. Map<any, any> was literally the intent. I'd strongly argue these cases should be explicit.
  2. The Map is constructed without contextual types, but it is meant to conform to them later. As a result, the code in-between those places is unsafe, as in the example below. Though unsafe, this might be a common pattern in practice that the change would break. (Ofc a solution is to explicitly type the Map construction.)
function getWordCounts(text: string): Map<string, number> {
    const m = new Map();
    for (const w of text.split(' ')) {
        m.set(w, (m.get(w) ?? 0) + 1);
    }
    return m;
}
Sample Code
// The problem is that this at-at-glance reasonable function is returning `any`.
function numRudeWords(input: string | null) {
    //   ^? function numRudeWords(input: string | null): any
    const wordCounts = input ? getWordCounts(input) : new Map();
    //    ^? const wordCounts: Map<any, any>
    return (wordCounts.get('meanie') ?? 0) + (wordCounts.get('dangnabit') ?? 0);
}

function getWordCounts(text: string): Map<string, number> {
    const m = new Map<string, number>();
    for (const w of text.split(' ')) {
        m.set(w, (m.get(w) ?? 0) + 1);
    }
    return m;
}
Documentation Link

The MDN doc link is here, though it's not super relevant to this particular question: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map

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 damit, die Deklarationen des Map-Konstruktors in lib.es2015.iterable.d.ts zu untersuchen, und sieh dir die verknüpfte issue #52552 zur bisherigen Diskussion an. Prüfe, wie sich das Entfernen der Überladung auf die Inferenz für leere Maps und die Beispiele in diesem Bericht auswirkt; erledigt bedeutet, dass die Deklarationsänderung und ihre Auswirkungen auf die Kompatibilität durch die relevanten Tests des Projekts abgedeckt sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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