Hey everyone. I am experienced with JavaScript but now getting into Typescript. Still trying to wrap my head around the whole thing.
I am creating a card game server. A particular type that I want is called Pending — to be used for scores and trump suits. For example, the Trump suit of a particular round will either be of type (enum) Suit, or it will be Pending (before it's dealt or otherwise decided).
I basically want to create a type (or a "value", I guess??) of Pending. So for example, when I create a new Round, it will be like:
interface Round = {
startingPlayer: number
trumpSuit: Suit | Pending
}
const round: Round = {
startingPlayer: 1,
trumpSuit: Pending
}
I also want this Pending thing to be falsy, so that I can make checks on it like if(!trumpSuit).
However, if I define Pending to be a type alias of undefined, TypeScript will give me the following error:
type Pending = undefined
const myVar = Pending // error: Pending is a type, but is referred to as a value here
How do I achieve what I want to achieve? I know that I'm probably misusing the idea of type aliases or literals. But basically I want to be able to pass a value of Pending (that underneath is just undefined or null), but a variable that is created as undefined shouldn't pass the type check of Pending, e.g
const round: Round = {
startingPlayer: 1,
trumpSuit: undefined // I want an error saying "can't be undefined. Must be Suit or Pending.
Any advice on how I should approach this? Should I just use a string literal of "pending"? In that case, how would I achieve the desired falsy behavior?