drwpow / cobalt-ui

Use W3C Design Token Community Group tokens in CI and code

Home Page:https://cobalt-ui.pages.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

typescript: token function typings

dsvgl opened this issue · comments

commented

Hello, it's me again 😆

your latest fix to #17 doesn't work as expected.

I doesn't complains about mode as second argument needed anymore. Great.

But I don't get intellisense for mode (tested with your apple example):
SCR-20221122-o6x

Also, I can put anything in both slots:
SCR-20221122-o79

Hello! I just fixed a TS error related to the token() function.

For your first problem, I’m not sure if intellisense is able to auto-fill the value. Sadly, you’ll just have to provide the value yourself, but it can tell you if the types don’t match.

For the 2nd problem—“no error”—I fixed a bug where it should throw a type error if you are trying to access a mode that doesn’t exist. Note that the signatures will have to match exactly, e.g.:

const mode = 'light'; // auto-cast as `string`
token('color.foreground', mode); // ❌ expecting `'light' | 'dark'`

const mode: 'light' | 'dark' = 'light';
token('color.foreground', mode); // ✅

In regards to runtime errors, however, it’s by design that this won’t throw a runtime error; it will just fall back to whatever the default value is. This is probably more generally useful, as tokens are meant to sometimes omit modes when they don’t need them.