#exported const file is empty after being built

32 messages · Page 1 of 1 (latest)

thin mason
#

I have a constant that is empty after being built with tsc and I don't know why or how I can fix this...

Here's my const:

// colors.ts

export declare const Colors: {
    Default: 0x000000;
    White: 0xffffff;
    Aqua: 0x1abc9c;
    Green: 0x57f287;
}

And my built version of it:

// colors.js

export {};

Why does this happen and how do I fix it?

frail grotto
#

Is this the only thing in the file? export default const Colors: { ... }

frail grotto
#

I'm not sure why you're using declare but that's probably your issue.

thin mason
#

It says 'TS1155: 'const' declarations must be initialized.'

#

That's why I use declare

frail grotto
#

ok... don't use declare.

thin mason
#

It says that without declare

frail grotto
#

Just do this:

export default const Colors = {
    Default: 0x000000;
    White: 0xffffff;
    Aqua: 0x1abc9c;
    Green: 0x57f287;
}
thin mason
#

TS1109: Expression expected

frail grotto
#

sorry... = not :

thin mason
#

Aaah thanks

#

Also can't use default there it seems

#

Works now

frail grotto
#

ok.. you should be able to use default once per file.

#

This will prevent you from having to use curly brackets when importing. So import Colors from colors.ts with default. Or, import {Colors} from colors.ts without default.

#

If you don't need to export multiple things it's preferred to use default

thin mason
#

Shouldn't I be using this then?:

const Colors = {
  Aqua: 0x1abc9c,
  Black: 0x000000,
  ....
};

export default Colors;
frail grotto
#

yes, either will work

thin mason
#

As export default const gives me 'TS1109: Expression expected.' again

frail grotto
#

yes you're right... you don't need to use const when with default.

thin mason
#

Good to know

frail grotto
#

This should work too:

export default {
    Default: 0x000000;
    White: 0xffffff;
    Aqua: 0x1abc9c;
    Green: 0x57f287;
}

import Colors from './colors'
thin mason
#

Thanks for your help ^^

#

At least it works now.

blissful zodiac
#

@thin mason just to clarify
declare tells the TypeScript compiler a certain variable exists, and you can give it a type
however, the variable won't be emitted (written in the JS code)
since this feature is primairly meant for testing purposes (when you want to mock a certain function without providing an implementation)

thin mason
#

Aaah

#

Thanks for the explanation

blissful zodiac
blissful zodiac
# thin mason It says 'TS1155: 'const' declarations must be initialized.'

you were getting that error because you were using the wrong syntax

const color: { Default: 0x000000 };

creates a variable with a type, but doesn't assign a value
it's the same as writing

const foo: string;

and since you can't re-assign a value later (since it is a const), you must initialize the variable when you create it
you probably meant

- const color: {
+ const color = {
    Default: 0x000000
  };
blissful zodiac
#

!resolved