-
Notifications
You must be signed in to change notification settings - Fork 414
feat: add typescript definitions #263
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
Conversation
4cd1dcf
to
33413b8
Compare
index.d.ts
Outdated
type StyledTag<T> = (strings: string[], ...exprs: Array<string | number | object | InterpolationFunction<T>>) => StyledComponent<T>; | ||
|
||
export const styled: { | ||
<T>(component: T): StyledTag<React.ElementConfig<T>>, |
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.
Here I want to return different types based on the type of T
, i.e. if T
is string
, StyledTag<{ children?: React.Node, [key: string]: any }>
, otherwise StyledTag<React.ElementConfig<T>>
index.d.ts
Outdated
|
||
export const styled: { | ||
<T>(component: T): StyledTag<React.ElementConfig<T>>, | ||
[tag: string]: StyledTag<{ children?: React.Node, [key: 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.
Here instead of a generic one, probably will be better if I could do something like:
div: StyledTag<JSX.InstrinsicElements.div>
But instead of having to do it for every tag, could I do it programmatically somehow? Like if this was Flow, I could do:
$ObjMap<JSX.InstrinsicElements, <T>(props: T) => StyledTag<T>>;
index.d.ts
Outdated
import * as React from 'react'; | ||
|
||
declare module "./index" { | ||
export const css: (strings: string[], ...exprs: Array<string | number | object>) => string; |
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.
Be consistent. Choose between string[]
and Array<string>
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.
@Esemesek can't man, it's union type
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.
@satya164 You can probably use (string | number | object)[]
6280724
to
91d65db
Compare
f0a7375
to
abec70c
Compare
abec70c
to
b42fdc6
Compare
typings/index.d.ts
Outdated
) => string; | ||
|
||
export const cx: ( | ||
...classNames: Array<string | false | undefined | null | 0> |
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.
Is 0 something special or can be replaced by number?
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.
It accepts strings or falsy values. That's why 0
is here, çoz it's falsy.
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.
Maybe you should extract to separate type to keep in semantically clean.
typings/__fixtures__/index.tsx
Outdated
@@ -0,0 +1,30 @@ | |||
/// <reference path="../index.d.ts" /> |
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.
Is this tripe-slash
thing required on top of each test file?
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.
Yeah, otherwise it can't find the module. Not sure if there's a way to avoid it. Do you have a suggestion?
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.
@satya164 You can include the files on types/tsconfig.json
like so:
{
"include": [
"index.d.ts",
"tests/**/*"
]
}
Note that you have to put these outside of compilerOptions
.
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.
@resir014 is this necessary? this comment is outdated. with the updated code I don't need this and typescript is checking the code properly
typings/index.d.ts
Outdated
export const styled: StyledJSXIntrinsics & { | ||
<T>(component: React.ReactType<T>): StyledTag<T>; | ||
|
||
[key: string]: StyledTag<{ |
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.
Use Record<string, SomeType>
instead of { [key: string]: SomeType }
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.
How would I use the record?
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.
It is more readable.
typings/index.d.ts
Outdated
) => string; | ||
|
||
export const cx: ( | ||
...classNames: Array<string | false | undefined | null | 0> |
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.
Maybe you should extract to separate type to keep in semantically clean.
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.
🔥
fb54c93
to
f424096
Compare
7d5c1a0
to
e818e69
Compare
e818e69
to
da4a94c
Compare
da4a94c
to
cc59fb9
Compare
cc59fb9
to
32bc2f5
Compare
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.
Overall, this looks good 👍 I left some comments for improvements (and a few others on some outdated diff), but this is good to go.
): string; | ||
|
||
function cx( | ||
...classNames: Array<string | false | undefined | null | 0> |
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.
This can probably be extracted into a separate type:
type FalseyValue = false | undefined | null | 0;
...classNames: (string | FalseyValue)[]
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.
@resir014 yeah, I did that in a previous commit but then reverted, because it seems that all the type aliases I declare are exported. so the user could do:
import { cx, Falsy} from 'linaria'
Which feels wrong :(
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.
ah, I see.
Thanks a lot for the review @resir014! |
I've never used typescript, so not sure if it's correct. But would be a good starting point if someone wants to help with typescript definitions.
fixes #197