Skip to content

Narrow a ternary expression to unique symbol typeΒ #63203

@mitar

Description

@mitar

πŸ” Search Terms

unique symbol
narrowing

βœ… Viability Checklist

⭐ 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions