-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript
Description
Consider following use case for string literals at the moment in Flux:
interface IAction {
actionType: string
}
interface ISayHelloAction extends IAction {
actionType: "SAY_HELLO";
}
// Somewhere in your code you check that the message is ISayHelloAction from that actionType string using normal if
function processActions(action: IAction|ISayHelloAction|...) {
if (action.actionType === "SAY_HELLO") {
// action is ISayHelloAction
}
}
(Above example assumes #6028 gets fixed, for the moment one can implement own type guard.)
But it would be even better if one could use Symbol for the same thing:
interface IAction {
actionType: Symbol
}
const SAY_HELLO: Symbol = Symbol();
interface ISayHelloAction extends IAction {
actionType: SAY_HELLO; // "Symbol literal type"
}
function processActions(action: IAction|ISayHelloAction|...) {
if (action.actionType === SAY_HELLO) {
// ... is ISayHelloAction
}
}
These symbol literal types has same type guard requirement as literal types.
sebald, mnpenner, disjukr, kaleb, korniychuk and 2 more
Metadata
Metadata
Assignees
Labels
Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript