microsoft / microsoft/TypeScript
Infer const-ness of local let bindings that aren't assigned to
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
Search Terms
let const infer assignment
Suggestion
In some cases, type narrowing for const bindings is more effective than for let bindings. Specifically, using such bindings from a function that closes over them will keep the narrowed type, which doesn't happen for let binding, presumably on the assumption that those might be mutated at any time.
This suggests to treat local let bindings that are never assigned to (which can be determined by a rather straightforward static check on the syntax tree) like const bindings for this purpose.
(Interestingly, something like this seems to already exist for function parameters.)
Use Cases
Though some people have adopted a coding style where locals are declared with const unless assigned to, there's also a good argument to be made that that is not a very good use of brain cycles and just using let for all locals is fine. This proposal would improve type inference for people using the let style.
Examples
function foo() {
let v: number[] | null = Math.random() < 0.5 ? [1] : null
if (Array.isArray(v)) setTimeout(() => console.log(v.length), 100)
}
The compiler complains that v is possibly null. If the binding is changed to const the problem goes away. If v is made a function parameter it also doesn't occur. (But comes back when a v = null statement is added, suggesting a check like the one suggested here is being done in that case.)
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit dem bereitgestellten foo-Beispiel und vergleiche das vorgeschlagene local-let-Verhalten mit dem bestehenden Verhalten für const-Bindings und Funktionsparameter. Definiere die Vervollständigung als das Beibehalten des eingegrenzten Typs innerhalb des abschließenden Callbacks, wenn ein lokales let-Binding keine Zuweisungen hat, während das bestehende Verhalten beibehalten wird, wenn es mutiert werden kann; füge Abdeckung für beide Fälle hinzu.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100