Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript interface for default export

I have a very basic folder structure for config files like this:

/config
  /button
  /colors
  /index

Code looks like this:

colors.ts

export interface Colors {
  [key: string]: string,
}

export default {
  black: '#111',
  grey: '#999',
  green: '#4c8857',
  red: '#bd1414',
  white: '#fff',
};

index.ts

import colors from './colors';

export default {
  button,
  colors,
};

I get the following error:

Could not find a declaration file for module './colors'. '/xxxxx/styles/config/colors.js' implicitly has an 'any' type.

I'm completely new to Typescript and can't find any tutorials or examples online that clearly explain what I've done wrong here.

like image 490
CaribouCode Avatar asked May 11 '26 20:05

CaribouCode


1 Answers

There is 2 problems: you are not importing the interface properly because of a case error:

import colors from './colors';
export interface Colors {

try

import {Colors} from './colors';

But here you are simply exporting the "Colors" interface, not the object of colours.

the code you might like would be something like:

export interface Colors {
  [key: string]: string,
}

export const defaultColors: Colors = {
  black: '#111',
  grey: '#999',
  green: '#4c8857',
  red: '#bd1414',
  white: '#fff',
};

Then import it

import {defaultColors} from './colors'

Note: if you struggle with imports, I strongly advise to use an IDE like Webstorm which can automatically import the right dependencies.

like image 183
Flavien Volken Avatar answered May 14 '26 12:05

Flavien Volken