Reputation: 12346
I have a Javascript function which accepts a number. Also there are some top-level constants:
var FOO = 1;
var BAR = 2;
and it only makes sense to call this function using one of these constants.
I want to create a type-safe interface for this function using enum:
declare enum MyType {
FOO,
BAR
}
interface MyClass {
process(MyType type);
}
but this code outputs MyType.FOO
in js file. I need it to output just FOO
but still be type-safe in typescript code. Is it possible?
Upvotes: 0
Views: 139
Reputation: 221192
// Version A
const enum _MyType {
FOO,
BAR
}
let FOO = _MyType.FOO;
let BAR = _MyType.BAR;
or
// Version B (if FOO and BAR come from another file)
declare const enum _MyType {
FOO,
BAR
}
declare let FOO: _MyType;
declare let BAR: _MyType;
Either way,
function fn(x: _MyType) { /* ... */ }
fn(FOO); // OK
fn('quack'); // Error
Upvotes: 2