microsoft / microsoft/TypeScript

Additive inverse of number literals

Aperta
#30,914 1 commento 5 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

Using unary minus on variable that is of number-literal type. Negative number literals.

Suggestion

This is a specific case of #26382. My proposal is simply that if we apply unary minus on an expression whose type is a union of number literal types, the result should be a union of their additive inverses:

declare var a: 1 | 2 | 3;
const b = -a; // should be -1 | -2 | -3

Furthermore we could add a unary minus type operator:

-(42) === -42
-(A | B) === -A | -B
-number === number
-(not number) // error

Both should be very easy and uncontroversial to implement.

Use Cases

function compare(a: C, b: C): -1 | 0 | 1;

function reverseCompare(a: C, b: C): -1 | 0 | 1
{
  return -compare(a, b); // shouldn't throw an error
}

The type operator would be just for sake of completeness

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 esaminando la proposta correlata nella issue #26382 e il comportamento del controllo dei tipi per il meno unario sulle unioni di valori letterali numerici. Il lavoro è completato quando gli esempi inferiscono -1 | -2 | -3 e reverseCompare viene accettato, mentre gli operandi non numerici non validi continuano a produrre errori; nella issue non vengono indicati file sorgente né test.

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

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.