Open yw662 opened 1 year ago
const
strings, booleans, and numbers are already treated as literals.
const
strings, booleans, and numbers are already treated as literals.
That's super cool. Although what I really what is to apply as const
by default to array literals and object literals.
I would appreciate this as one of the strict mode options: when assigning to a const
variable or a readonly
member the compiler should infer that as const
should be applied.
To be honest, I kinda wish there was a way that you could flip the mutability defaults across the whole language (perhaps introducing a mut
keyword to opt in to mutability in given cases) but that's unlikely to happen. I'll settle for this!
This seems similar to #42357
I want this too -- here's my real-life example
// this thing has a definite type
const value: "speed" | "direction" | "both";
// now I want to know which it is, but i've made a typo
if (["direction", "boooo"].includes(value)) {
// do the thing
}
TS will not help me here, unless I write (["direction", "boooo"] as const).includes(...)
. That's annoying, and everyone on the team has to remember to write "as const" forever more.
I'd rather turn on a "use const" mode per-file or across the whole project, and opt out of it with string[]
type declarations when necesssary.
Thanks!
Suggestion
🔍 Search Terms
const by default, const mode, use const, as const
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
An optional
const mode
, manually enabled bytsconfig
or cli options or maybe a"use const"
directive. In const mode, a selective subset of js literals (like all primitive literals, and usually arrays) areas const
by default, eg.or some alternative directives:
When the non-const type is wanted:
📃 Motivating Example
Literals, esp. primitive literals assigning to constant variables usually have a good chance to remain what they are during their whole lifetime. Applying
as const
by default can further improve type safety and convenience in projects where type safety is valued.💻 Use Cases
Literally to type less
as const
.