-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Description
π Search Terms
unique symbol
narrowing
β Viability Checklist
- 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, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
β Suggestion
Support creating unique symbols with expressions which always evaluate to the same symbol in runtime.
π Motivating Example
I want to use:
export const NONE: unique symbol = process.env.NODE_ENV !== "production" ? Symbol.for("none") : Symbol()
process.env.NODE_ENV does not change during execution. So this will always produce one unique symbol during runtime.
π» Use Cases
During development, Vite can optimize dependencies and can duplicate imports and thus symbols. So we use Symbol.for to make sure that symbols are deduplicated. Also symbol name is useful for debugging. But in production this is not needed.
Vite can be configured to disable dependency optimizations, but we believe it is better that not all consumers of our code have to do that. Instead, we want to fix this inside the code itself.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels