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
type colorsType = keyof colors;but then colors may need to be changed to something - Grundytype colorsType = keyof typeof colors;seems to work in a sandbox - Grundy