I have an interface Action
:
interface Action {}
And an implementation of this Action
SpecificAction
:
class SpecificAction implements Action { payload?: Any }
Is it possible in TS to construct a switch operator, like this:
let action: Action switch (action) { case SpecificAction: //it works console.log(action.payload) // it doesn't }
Is it possible in that case to know, that action is already of SpecificAction
type?
The switch statement is used to check for multiple values and executes sets of statements for each of those values. A switch statement has one block of code corresponding to each value and can have any number of such blocks.
TypeScript allows you to define multiple types. The terminology for this is union types and it allows you to define a variable as a string, or a number, or an array, or an object, etc. We can create union types by using the pipe symbol ( | ) between each type.
The is keyword is actually casting the type and can catch type errors later in the code. See this example for more info. @benjaminz I don't see how it could be handled by a boolean. Typescript needs to know that the function into which you pass an object is functioning like a type guard.
for the time being it looks like there are a few options, all of them with some drawbacks
interface Action {} class SpecificAction implements Action { kind: "specific"; payload?: any; } class ToggleAction implements Action { kind: "toggle"; toggle: boolean; } let action: SpecificAction | ToggleAction; switch (action.kind) { case "specific": console.log(action.payload) // it works break; case "toggle": console.log(action.toggle) // it works break; }
interface Action {} class SpecificAction implements Action { payload?: any; } class ToggleAction implements Action { toggle: boolean; } let isSpecific = (p: any): p is SpecificAction => !!p.payload let isToggle = (p: any): p is ToggleAction => !!p.toggle let action: Action; if (isSpecific(action)) { console.log(action.payload) // it works } else if (isToggle(action)) { console.log(action.toggle) // it works }
interface Action { } class SpecificAction implements Action { payload?: any; } class ToggleAction implements Action { toggle: boolean; } switch (action.constructor) { case SpecificAction: console.log((<SpecificAction>action).payload) // it kinda works break; case ToggleAction: console.log((<ToggleAction>action).toggle) // it kinda works break; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With