Worded an other way:
How would you type the windowState DOM property in TypeScript?
SOLVED (in TypeScript 2):
declare var windowState: WindowState
const enum WindowState {
STATE_MAXIMIZED = 1,
STATE_MINIMIZED = 2,
STATE_NORMAL = 3,
STATE_FULLSCREEN = 4
}
...
var windowState = 5 // Type Error, as expected!
Original question:
How do I declare a type in TypeScript so that it describes an algebraic data type? The purpose of this is describing an existing API.
When I try the following, TypeScript obviously complains that a type is expected:
type Weather = 'sunny' | 'bad'
One idea I had is using a JavaScript 2015 Symbol, however TypeScript doesn't seem to know about these.
An other idea was using an enum, however TypeScript complains that a member initializer must be constant expression:
const enum Weather {
sunny = 'sunny',
bad = 'bad',
windy = Symbol('windy')
}
I would have thought that a string constant is a constant expression.