-
Notifications
You must be signed in to change notification settings - Fork 566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add TypeScript typings #410
Conversation
types/index.d.ts
Outdated
declare namespace StyleDictionary { | ||
interface Property { | ||
value: string; | ||
[attribute: string]: any; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been away from typescript for awhile. I think that one goal is to avoid the use of 'any'.
Is it possible that this should be "string"? Or does it make sense in this context because this is how SD is structured?
If you have pointers to the logic here would be helpful for me to validate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is best practise to avoid any
, however I used it here as the config JSON can contain any additional attribute. Thinking about this a bit more perhaps it would be better scoped to the types valid in JSON?
Perhaps something like this:
type JSONValue = null | string | JSONValue[] | JSONObject;
interface JSONObject {
[key: string]: JSONValue;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also looking at this https://github.com/AndrewLeedham/style-dictionary/blob/master/lib/transform/propertySetup.js#L35 I think the internal type I called Prop
should also have this behaviour.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The JSONObject
idea is not feasible. Unfortunately because other properties exist on the interfaces, an index signature would have to include non-valid props as well.
The built in JSON.parse
function returns any
so I think we will have to use any
here.
Hey, this looks pretty good. I'm a bit rusty on my TS, can you help remind me on that one comment? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Going to delay merging for now. We would be adding tests to everyones systems, even those who don't use typescript. We want to play with things a bit to make sure we don't impact those folks. |
Have there been any other volunteers who've confirmed they've tried using this branch already? Thx |
Hi Andrew - sorry about the delay here. I'm relooking at this, and I've got a couple concerns that if we can resolve, we can merge this.
|
tsd is not deprecated to my knowledge it was updated a few months ago, it is still recommended on the official docs https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html#tips and by the Microsoft linting plugin used in DefinitielyTyped: https://github.com/microsoft/dtslint#just-looking-for-expecttype-and-expecterror |
@luke-john FYI |
Nice work 💖 💯 , thanks. I've just tested this on our usage of style-dictionary at Bumble and it works beautifully. Will mean we can get rid of a lot of anys and a custom definition we'd written for style-dictionary. The only change we'll need to make use of these, is to return a boolean rather than a falsy value from the matcher in matcher: function (prop) {
- return prop.type === 'time' && prop.value.match(/^[\d.]+$/);
+ return prop.type === 'time' && !!prop.value.match(/^[\d.]+$/);
}, The documentation does state that the matcher should return false though, so makes sense to have the types be strict about this 👍 https://amzn.github.io/style-dictionary/#/api?id=registertransform |
LGTM |
Issue #, if available:
Description of changes:
types/index.d.ts
which provides typings forstyle-dictionary
when used in a TypeScript environment.types
field to thepackage.json
so TypeScript knowns where the typings are.types/index.d.ts
to thefiles
field in thepackage.json
so it is published as part of the npm module.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.