The project uses sass for styling and typescript for typing.

There is a set of constants that is used in both styles and interfaces of reactor components. These are colors.

Now there is a separate colors.scss file

 $white: white; $brightest: #f0f0f0 ... 

And enum

 enum Colors { WHITE = 'white', BRIGHTEST = 'brightest', ... } 

I want to avoid the support of two files. Whether it is possible how that in a typing type?

Tried so

colors.scss

 $white: white; $brightest: #f0f0f0 :export { white: $white; brightest: $brightest; } 

somefile.ts

 import colors from "colors.scss"; function literalArray<K extends string>(args: K[]): K[] { return args; } const ProxyProperties = literalArray(Object.keys(colors)); // так не работает // const ProxyProperties = literalArray(['white', 'brightest']); // а так работает type colorsType = typeof ProxyProperties[number]; ((v: colorsType) => console.log(v))("white"); ((v: colorsType) => console.log(v))("red"); ((v: colorsType) => console.log(v))(''); 

With this approach, the type is generated only if you explicitly pass an array to the function, if you get it through Object.keys then instead of

 type colorsType = 'white' | 'brightest' 

we get

 type colorsType = string 
  • and what does Object.keys (colors) display? - Grundy
  • exactly what is needed. array, which, if entered directly gives the desired result - torokhkun
  • instead of importing from scss, I tried to take out the name and meaning of colors in a json object, and then import it into scss and ts. But still it does not work out of the keys of this object to get the union type - torokhkun
  • you can try type colorsType = keyof colors; but then colors may need to be changed to something - Grundy
  • And you can also try this: type colorsType = keyof typeof colors; seems to work in a sandbox - Grundy

0