Reputation: 1125
In Car.ts, I have the following code:
export class Car {
static SMALL: string = "small";
static MEDIUM: string = "medium";
static LARGE: string = "large";
}
In CarSize.ts, I have the following code:
import { Car } from "./Car";
type CARSIZE = Car.SMALL | Car.MEDIUM | Car.LARGE;
I'm trying to create a CARSIZE type which can only be one of Car.SMALL
, Car.MEDIUM
or CAR.LARGE
.
I receive the following error:
'Car' only refers to a type, but is being used as a namespace here.
How do I fix this error?
Upvotes: 1
Views: 125
Reputation: 37918
Because Car.SMALL
is a value, not a type.
I'd use string enum in this case:
enum CarSize {
small = "small",
medium = "medium",
large = "large"
}
But if you really want to get this working, you can use typeof
to get the type of the value. Also you'll need to define properties as readonly
and remove explicit type notation, so compiler will be able to infer the types as literals:
class Car {
static readonly SMALL = "small";
static readonly MEDIUM = "medium";
static readonly LARGE = "large";
}
type CARSIZE = typeof Car.SMALL | typeof Car.MEDIUM | typeof Car.LARGE;
Upvotes: 4