Skip to content

Commit

Permalink
PropsOf<C> TypeScript utilities - Support defaultProps (#1405)
Browse files Browse the repository at this point in the history
* PropsOf<C> TypeScript utilities - Support defaultProps by delegating to new JSX and React types.

* changeset

* Regenerated yarn.lock without internal urls

* Align versions of @types/react across project by upgrading @emotion/core and root package versions.
  • Loading branch information
ryanswanson authored and emmatown committed Jun 20, 2019
1 parent 4ae8894 commit c673e20
Show file tree
Hide file tree
Showing 17 changed files with 148 additions and 68 deletions.
7 changes: 7 additions & 0 deletions .changeset/tasty-hornets-taste/changes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"releases": [
{ "name": "emotion-theming", "type": "patch" },
{ "name": "@emotion/styled-base", "type": "patch" }
],
"dependents": []
}
1 change: 1 addition & 0 deletions .changeset/tasty-hornets-taste/changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PropsOf<C> TypeScript utilities - Support defaultProps by delegating to new JSX and React types.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"@changesets/get-github-info": "^0.1.2",
"@types/jest": "^23.0.2",
"@types/node": "^10.11.4",
"@types/react": "16.3.18",
"@types/react": "^16.8.20",
"babel-check-duplicated-nodes": "^1.0.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
Expand Down
6 changes: 3 additions & 3 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@
"test:typescript": "dtslint types"
},
"dependencies": {
"@babel/runtime": "^7.4.3",
"@emotion/cache": "^10.0.9",
"@emotion/css": "^10.0.9",
"@emotion/serialize": "^0.11.6",
"@emotion/sheet": "0.9.2",
"@emotion/utils": "0.11.1",
"@babel/runtime": "^7.4.3"
"@emotion/utils": "0.11.1"
},
"peerDependencies": {
"react": ">=16.3.0"
},
"devDependencies": {
"@emotion/styled": "^10.0.10",
"@types/react": "16.3.18",
"@types/react": "^16.8.20",
"dtslint": "^0.3.0",
"emotion": "^10.0.9",
"emotion-server": "^10.0.9",
Expand Down
6 changes: 2 additions & 4 deletions packages/core/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export interface GlobalProps<Theme> {
* @desc
* JSX generic are supported only after TS@2.9
*/
export function Global<Theme = any>(
props: GlobalProps<Theme>
): ReactElement<any>
export function Global<Theme = any>(props: GlobalProps<Theme>): ReactElement

export function keyframes(
template: TemplateStringsArray,
Expand Down Expand Up @@ -79,7 +77,7 @@ export interface ClassNamesProps<Theme> {
*/
export function ClassNames<Theme = any>(
props: ClassNamesProps<Theme>
): ReactElement<any>
): ReactElement

declare module 'react' {
interface DOMAttributes<T> {
Expand Down
43 changes: 20 additions & 23 deletions packages/core/types/tslint.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"array-type": [
true,
"generic"
],
"import-spacing": false,
"semicolon": false,
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-rest-spread",
"check-type",
"check-typecast",
"check-type-operator",
"check-preblock"
],

"no-unnecessary-generics": false
}
"extends": "dtslint/dtslint.json",
"rules": {
"array-type": [true, "generic"],
"import-spacing": false,
"semicolon": false,
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-module",
"check-rest-spread",
"check-type",
"check-typecast",
"check-type-operator",
"check-preblock"
],
"no-null-undefined-union": false,
"no-unnecessary-generics": false
}
}
10 changes: 5 additions & 5 deletions packages/emotion-theming/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@
"devDependencies": {
"@emotion/core": "^10.0.10",
"@emotion/styled": "^10.0.10",
"@types/react": "16.3.18",
"@types/react": "^16.8.20",
"dtslint": "^0.3.0"
},
"dependencies": {
"@babel/runtime": "^7.4.3",
"@emotion/weak-memoize": "0.2.2",
"hoist-non-react-statics": "^3.3.0",
"@babel/runtime": "^7.4.3"
"hoist-non-react-statics": "^3.3.0"
},
"peerDependencies": {
"react": ">=16.3.0",
"@emotion/core": "^10.0.0"
"@emotion/core": "^10.0.0",
"react": ">=16.3.0"
},
"umd:main": "dist/emotion-theming.umd.min.js",
"browser": {
Expand Down
12 changes: 3 additions & 9 deletions packages/emotion-theming/types/helper.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import * as React from 'react'

export type PropsOf<C extends React.ComponentType<any>> = C extends React.SFC<
infer P
>
? P & React.Attributes
: C extends React.ComponentClass<infer P>
? (C extends new (...args: Array<any>) => infer I
? P & React.ClassAttributes<I>
: never)
: never
export type PropsOf<
C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
> = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithRef<C>>

export type Omit<T, U> = T extends any ? Pick<T, Exclude<keyof T, U>> : never
export type AddOptionalTo<T, U> = Omit<T, U> &
Expand Down
6 changes: 3 additions & 3 deletions packages/emotion-theming/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.8
// TypeScript Version: 3.1

import * as React from 'react'

Expand All @@ -12,7 +12,7 @@ export interface ThemeProviderProps<Theme> {

export function ThemeProvider<Theme>(
props: ThemeProviderProps<Theme>
): React.ReactElement<any>
): React.ReactElement

/**
* @todo Add more constraint to C so that
Expand All @@ -23,7 +23,7 @@ export function withTheme<C extends React.ComponentType<any>>(
): React.SFC<AddOptionalTo<PropsOf<C>, 'theme'>>

export interface EmotionTheming<Theme> {
ThemeProvider(props: ThemeProviderProps<Theme>): React.ReactElement<any>
ThemeProvider(props: ThemeProviderProps<Theme>): React.ReactElement
withTheme<C extends React.ComponentType<any>>(
component: C
): React.SFC<AddOptionalTo<PropsOf<C>, 'theme'>>
Expand Down
17 changes: 17 additions & 0 deletions packages/emotion-theming/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ const ThemedComp = withTheme(CompC)
;<ThemedComp prop />
;<ThemedComp prop theme={theme} />

const CompSFCWithDefault = ({ prop }: Props) => (prop ? <span /> : <div />)
CompSFCWithDefault.defaultProps = { prop: false }
class CompCWithDefault extends React.Component<Props> {
static defaultProps = { prop: false }
render() {
return this.props.prop ? <span /> : <div />
}
}

const ThemedSFCWithDefault = withTheme(CompSFCWithDefault)
;<ThemedSFCWithDefault />
;<ThemedSFCWithDefault theme={theme} />

const ThemedCompWithDefault = withTheme(CompCWithDefault)
;<ThemedCompWithDefault />
;<ThemedCompWithDefault theme={theme} />

const {
ThemeProvider: TypedThemeProvider,
withTheme: typedWithTheme
Expand Down
2 changes: 1 addition & 1 deletion packages/emotion-theming/types/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"check-type-operator",
"check-preblock"
],

"no-null-undefined-union": false,
"no-unnecessary-generics": false
}
}
4 changes: 2 additions & 2 deletions packages/styled-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@emotion/utils": "0.11.1"
},
"devDependencies": {
"@types/react": "16.3.18",
"@types/react": "^16.8.20",
"dtslint": "^0.3.0"
},
"peerDependencies": {
Expand All @@ -40,4 +40,4 @@
"preconstruct": {
"umdName": "emotionStyledBase"
}
}
}
10 changes: 2 additions & 8 deletions packages/styled-base/types/helper.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import * as React from 'react'
* @desc Utility type for getting props type of React component.
*/
export type PropsOf<
Tag extends React.ComponentType<any>
> = Tag extends React.SFC<infer Props>
? Props & React.Attributes
: Tag extends React.ComponentClass<infer Props>
? (Tag extends new (...args: Array<any>) => infer Instance
? Props & React.ClassAttributes<Instance>
: never)
: never
C extends keyof JSX.IntrinsicElements | React.JSXElementConstructor<any>
> = JSX.LibraryManagedAttributes<C, React.ComponentPropsWithRef<C>>

export type Omit<T, U> = T extends any ? Pick<T, Exclude<keyof T, U>> : never
export type Overwrapped<T, U> = Pick<T, Extract<keyof T, keyof U>>
2 changes: 1 addition & 1 deletion packages/styled-base/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.8
// TypeScript Version: 3.1

/**
* @desc
Expand Down
24 changes: 24 additions & 0 deletions packages/styled-base/types/tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,27 @@ declare const ref3_2: (element: HTMLDivElement | null) => void
;<Readable kind="magazine" author="Hejlsberg" /> // $ExpectError
;<StyledReadable kind="magazine" author="Hejlsberg" /> // $ExpectError
}

interface Props {
prop: boolean
}
class ClassWithDefaultProps extends React.Component<Props> {
static defaultProps = { prop: false }
render() {
return this.props.prop ? <Button0 /> : <Button1 />
}
}
const StyledClassWithDefaultProps = styled(ClassWithDefaultProps)`
background-color: red;
`
const classInstance = <StyledClassWithDefaultProps />

const FCWithDefaultProps = ({ prop }: Props) =>
prop ? <Button0 /> : <Button1 />
FCWithDefaultProps.defaultProps = {
prop: false
}
const StyledFCWithDefaultProps = styled(FCWithDefaultProps)`
background-color: red;
`
const fcInstance = <StyledFCWithDefaultProps />
2 changes: 1 addition & 1 deletion packages/styled-base/types/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"check-type-operator",
"check-preblock"
],

"no-null-undefined-union": false,
"no-unnecessary-generics": false
}
}
Loading

0 comments on commit c673e20

Please sign in to comment.