1

I've got a typescript/react project in which I am using @material components. There are definitions for those under @types/material__component. In the definitions there are often interfaces like (this one is from @types/material__menu/constants.d.ts):

export interface CornerBit extends MDCNumbers {
    BOTTOM: 1;
    CENTER: 2;
    RIGHT: 4;
    FLIP_RTL: 8;
}

Now if I wanna use CornerBit in my code I'd do:

import { CornerBit } from "@material/menu/"

@material/menu/index.js exports CornerBit from constants.js in which it is declared as:

const CornerBit = {
  BOTTOM: 1,
  CENTER: 2,
  RIGHT: 4,
  FLIP_RTL: 8,
};

Now when I import CornerBit like written above into my file and try to do:

const MyCornerBit = CornerBit;

I get:

TS2693: 'Corner' only refers to a type, but is being used as a value here.

So I guess I'm importing just the interface somehow, not the const itself. How do I use an interface like that (all properties have explicit values) though?

That just feels completely useless:

const MyCornerBit: CornerBit = {
    BOTTOM: 1,
    CENTER: 2,
    RIGHT: 4,
    FLIP_RTL: 8,
}

Thank you.

2
  • You’re trying to assign CornerBit to MyCornerBit and as the error says that’s not ok since it’s a type. If you want to say MyCornerBit is of type CornerBit then use : instead of =. It’s a bit unclear what you’re trying to achieve Commented Jul 31, 2018 at 9:47
  • Im trying to make MyCornerBit be an object with the values specified above. So that I can use MyCornerBit.BOTTOM, MyCornerBit.CENTER etc. Commented Jul 31, 2018 at 10:10

1 Answer 1

1

You should just declare your const MyCornerBit as a CornerBit like this

const MyCornerBit: CornerBit;

There is no need to instantiate that const with matching properties

Sign up to request clarification or add additional context in comments.

7 Comments

When I do const MyCornerBit: CornerBit; I get TS1155: 'const' declarations must be initialized
declare with let instead const
Hmm and then I need to disable tslint, because it tells me to use const when MyCornerBit is not being reassigned. But is is actually working. So thank you. Still seems a little strange to me though.
Okay, sorry. It is NOT working actually. Getting no compile time errors or anything, but runtime is telling me "Cannot read BOTTOM from undefined"
Does this interface CornerBit have an injectable decorator?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.