microsoft / microsoft/TypeScript

Feature Request: In JS, check if the non-widened type of an object property satisfies type constraints

Aperta
#38,056 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

Search Terms

javascript as const, expando literal

Suggestion

I am providing declaration files that are mainly consumed by JS projects. The API I'm writing these declaration files for is very strict in what it accepts and I am mirroring that in the declarations. Users often stumble across assignments that seem perfectly valid, but are not accepted by TypeScript. The reason is the widening from literal types to string.

Here's a (dumbed down) example:

/** @param {{prop1: "a" | "b"}} arg */
function foo(arg) {}

const arg = {};
arg.prop1 = "a";
foo(arg); // error here: string is not compatible with "a" | "b".

const arg2 = {
	prop1: "b"
};
foo(arg2); // Also an error

While in TypeScript, I can just put as const behind the assignment, there's no such thing in JS. Therefore I propose that when type-checking such constructs, the implicit literal type (here: prop1: "a", resp. prop1: "b") is taken into account.

Use Cases

Simplifying usage of strictly-typed libraries from JavaScript. Many JS users don't know what to do with those kinds of error messages and assume that there's a bug in the library or type definitions.

Examples

Here's how it could work:

const arg2 = {
	prop1: "b"
};
foo(arg2); // should be fine, arg2.prop1 should be considered as literal `"b"` for this call

arg2.prop1 = "a";
foo(arg2); // should still be fine, CFA should know that prop1 is of type `"a"`

arg2.prop1 = someFunctionThatReturnsString();
foo(arg2); // should be an error, since prop1 can now be any string

arg2.prop1 = "a";
foo(arg2); // should be fine again, since the last assignment was the literal `"a"`

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code - It would change existing behavior, but I would argue it is not breaking.
  • 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 riproducendo gli esempi JavaScript dell’issue e confronta le diagnostiche attuali con il comportamento proposto per le assegnazioni a proprietà letterali. Definisci le regole di controllo dei tipi per le assegnazioni letterali, la riassegnazione a partire da una funzione che restituisce una stringa e il restringimento tramite il flusso di controllo; il lavoro è completato quando gli esempi producono le diagnostiche richieste 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à
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.