microsoft / microsoft/TypeScript

Add missing `toString` declarations for base types that have them

Aperta
#38,347 8 commenti 25 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

toString, RegExp, Regular Expression, Boolean, Error

Suggestion

In @typescript-eslint, we have a rule no-base-to-string. This rule ensures that any value that's being coerced to a string has a toString method defined on it.
This is to help catch cases where you accidentally coerce something to a string, and end up with an [object Object] in your strings.

This rule has a pretty simple implementation - it just gets the declarations of the toString method on the type, and ensures that none of the declarations belong to the Object type.
The logic relies upon the fact that every type that actually has a toString method explicitly declares a toString method.

A few of our users ran into cases that were reporting errors for them, even though they shouldn't (https://github.com/typescript-eslint/typescript-eslint/issues/1655).
There's one that specifically has caused people some issues: Boolean/boolean.

I eyeballed the docs and the types, and I am pretty certain there are only 3 types that are missing a toString declaration:

Related

PR: #37839

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 individuando le dichiarazioni della libreria standard per RegExp, Boolean ed Error e confronta i membri esistenti con i corrispondenti built-in di JavaScript. Verifica che ogni dichiarazione toString mancante sia rappresentata nei tipi e verifica la modifica con la suite di test TypeScript pertinente; il lavoro è completato quando tutti e tre i tipi dichiarano esplicitamente il metodo senza modificare il JavaScript emesso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.