Reputation: 34489
I'm experimenting with TypeScript for the first time (and modules for that fact). I get the principle behind modules but whenever I try to export a type
I seem to be having problems.
For example given the following:
export type typeOne = "A" | "B" | "C" | "D";
When compiled essentially gives me empty output, leaving me with just a "use strict"
in the generated file which I don't understand.
What I originally started trying to do was to export several enum
types in a Constants
object:
export Constants {
type typeOne = "A" | "B" | "C" | "D";
type typeTwo = "X" | "Y" | "Z";
}
But seems I can't even get the basic case working. Am I missing something really obvious, or am I hitting some sort of restriction in TypeScript?
Upvotes: 0
Views: 86
Reputation: 164139
As javascript isn't a typed language, all of the declared types are being removed in the compilation process.
The types you defined are just strings that the compiler will check against specific set of values.
These three functions:
function fn1(value: "A" | "B" | "C"): boolean {
return value === "A" || value === "B" || value === "C";
}
function fn2(value: string): boolean {
return value === "A" || value === "B" || value === "C";
}
function fn3(value): boolean {
return value === "A" || value === "B" || value === "C";
}
Will be compiled to the same js function:
function fnN(value) {
return value === "A" || value === "B" || value === "C";
}
The difference is in compilation time (and in any normal IDE):
fn1("no good");
Will result in compilation error as the passed string is neither of the 3 specific values we defined, but fn2
and fn3
will be ok with that value.
Upvotes: 1