microsoft / microsoft/TypeScript

Narrow type of variable when declared as literal (also tuples)

Ouverte
#16,896 6 commentaires 23 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Needs Proposal Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

TypeScript Version: 2.4.1

Code

The following doesn't compile, because x has inferred type string. I think it would be helpful if it did, but I still want x to have inferred type string:

function blah(arg: "foo" | "bar") {
}
let x = "foo";
blah(x);
x = "something else";

Desired behavior:

This would compile.

Actual behavior:

demo.ts(4,6): error TS2345: Argument of type 'string' is not assignable to parameter of type '"foo" | "bar"'.

Suggestion

The type of x should be inferred to be string, but it should be narrowed to "foo" via flow-sensitive typing, much like the below program:

function blah(arg: "foo" | "bar") {
}
let x = "foo";
if (x != "foo") throw "impossible"; // NOTE: codegen would NOT insert this
blah(x);
x = "something else";

Note that this program currently works as desired: x is still inferred to have type string, but the guard immediately narrows it to type "foo". The assignment after the function call widens x's type back to string.

My suggestion is for TS to treat declarations that assign variables to literals (as in the first example) to automatically perform this flow-sensitive type narrowing, without the need for an unnecessary guard.

I'm suggesting it only for declarations, not general assignments (so only with let, var, and const) or other operations. In addition, I'm only suggesting it for assignments to literal values (literal strings or literal numbers) without function calls, operations, or casts.

Syntax Changes

Nothing changes in syntax.

Code Generation

Nothing changes in code generation.

Semantic Changes

Additional flow-sensitive type narrowing occurs when variables are declared as literals, essentially making the existing

let x = "foo";

behave the same as (in checking, but not in codegen)

let x = "foo";
if (x != "foo") throw "unreachable";

Reverse Compatibility

Due to x being assigned a more-precise type than it was in the past, some code is now marked as unreachable/invalid that previously passed:

let x = "foo";
if (x == "bar") { // error TS2365: Operator '==' cannot be applied to types '"foo"' and '"blah"'.
  // ...
}

The error is correct (in that the comparison could never succeed) but it's still possibly undesirable.

I don't know how commonly this occurs in production code for it to be a problem. If it is common, this change could be put behind a flag, or there could be adjustments made to these error cases so that the narrowing doesn't occur (or still narrows, but won't cause complaints from the compiler in these error cases).

If the existing behavior is strongly needed, then the change can be circumvented by using an as cast or an indirection through a function, although these are a little awkward:

let x = "foo" as string;
let x = (() => "foo")()

Tuples and Object Literals

It would be helpful if this extended also to literal objects or literal arrays (as tuples).

For example, the following could compile:

function blah(arg: [number, number]) {
}
let x = [1, 3];
blah(x);
x = [1, 2, 3];

with x again having inferred type number[] but being flow-typed to [number, number]. The same basic considerations apply here as above.

Similarly, it could be useful for objects to have similar flow-typing, although I'm not sure if this introduces new soundness holes:

function get(): number {
    return 0;
}
function blah(arg: {a: "foo" | "bar", b: number}) {
    // (...)
}

let y = get(); // y: number
let x = {a: "foo", b: y}; // x: {a: string, b: number}
// x is narrowed to {a: "foo", b: number}

blah(x); // this compiles due to x's narrowed type

x.a = "something else"; // this is accepted, because x: {a: string, b: number}.

See https://github.com/Microsoft/TypeScript/issues/16276 and https://github.com/Microsoft/TypeScript/issues/16360 and probably others for related but different approaches to take here.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

L’issue ne mentionne aucun fichier, test ou point d’entrée. Commencez par examiner les exemples proposés de littéraux, de tuples et d’objets, ainsi que les issues associées 16276 et 16360. Le travail est terminé lorsque le flow narrowing au moment de la déclaration fonctionne pour les cas demandés, tandis que les affectations ultérieures conservent le type inféré plus large.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.