From f47f8eeea9e4691ebe5e93db524c3f645de10546 Mon Sep 17 00:00:00 2001 From: Mohsen Azimi Date: Mon, 16 Sep 2019 15:32:59 -0400 Subject: [PATCH] Add types for react-with-styles (#38335) * Add typings for react-with-styles * Add typings for react-with-styles * Add typings for react-with-styles * fix header * Fix issues --- types/react-with-styles/index.d.ts | 70 +++++++++++++++++++ .../lib/ThemedStyleSheet.d.ts | 14 ++++ .../react-with-styles-tests.ts | 9 +++ types/react-with-styles/tsconfig.json | 24 +++++++ types/react-with-styles/tslint.json | 1 + 5 files changed, 118 insertions(+) create mode 100644 types/react-with-styles/index.d.ts create mode 100644 types/react-with-styles/lib/ThemedStyleSheet.d.ts create mode 100644 types/react-with-styles/react-with-styles-tests.ts create mode 100644 types/react-with-styles/tsconfig.json create mode 100644 types/react-with-styles/tslint.json diff --git a/types/react-with-styles/index.d.ts b/types/react-with-styles/index.d.ts new file mode 100644 index 0000000000..f7d67d4ce5 --- /dev/null +++ b/types/react-with-styles/index.d.ts @@ -0,0 +1,70 @@ +// Type definitions for react-with-styles 4.0 +// Project: https://github.com/airbnb/react-with-styles#readme +// Definitions by: Mohsen Azimi +// Brie Bunge +// Joe Lencioni +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import * as React from 'react'; +import * as PropTypes from 'prop-types'; +import { CSSProperties } from 'aphrodite'; + +interface Theme { [key: string]: any; } + +interface WithStylesProps { + /** + * This function takes styles that were processed by `withStyles()`, plain objects, or arrays + * of these things. It returns an object with an opaque structure that must be spread into a + * JSX element. + */ + css(...styles: any[]): object; + styles: { [key: string]: object }; + theme: T; +} + +declare const withStylesPropTypes: PropTypes.ValidationMap; + +type Nullable = T | null | undefined; +interface Styles { + [key: string]: Nullable< + CSSProperties & { + [pseudoSelectorOrMediaQuery: string]: CSSProperties[keyof CSSProperties] | CSSProperties; + } + >; +} + +interface WithStylesOptions { + flushBefore?: boolean; + pureComponent?: boolean; +} + +type Omit = Pick>; +type ComponentClassProps = C extends new (props: infer P, context?: any) => any ? P : never; +type SFCProps = C extends (props: infer P & { children?: React.ReactNode }, context?: any) => any ? P : never; +type ElementProps = C extends React.ComponentClass + ? ComponentClassProps + : C extends React.SFC + ? SFCProps + : any; +type ElementConfig = JSX.LibraryManagedAttributes>; + +declare function withStyles( + styleFn?: ((theme: T) => Styles) | null, + options?: WithStylesOptions, +): >( + component: C, +) => React.ComponentClass, keyof WithStylesProps>>; + +declare function css(...styles: any[]): object; + +export { + css, + withStyles, + WithStylesProps, + withStylesPropTypes, + WithStylesOptions, + Theme, + Styles, + CSSProperties, +}; diff --git a/types/react-with-styles/lib/ThemedStyleSheet.d.ts b/types/react-with-styles/lib/ThemedStyleSheet.d.ts new file mode 100644 index 0000000000..e59ff678c3 --- /dev/null +++ b/types/react-with-styles/lib/ThemedStyleSheet.d.ts @@ -0,0 +1,14 @@ +interface ThemedStyleSheet { + registerTheme: (theme: any) => void; + registerInterface: (registerInterface: any) => void; + create: (makeFromTheme: any, createWithDirection: any) => any; + createLTR: (makeFromTheme: any) => any; + createRTL: (makeFromTheme: any) => any; + get: () => any; + resolve: () => any; + resolveLTR: () => any; + resolveRTL: () => any; + flush: () => void; +} + +export default ThemedStyleSheet; diff --git a/types/react-with-styles/react-with-styles-tests.ts b/types/react-with-styles/react-with-styles-tests.ts new file mode 100644 index 0000000000..1835fbbeed --- /dev/null +++ b/types/react-with-styles/react-with-styles-tests.ts @@ -0,0 +1,9 @@ +import * as React from 'react'; +import { withStyles, WithStylesProps, css } from 'react-with-styles'; + +const Component: React.FC = ({ styles }) => + React.createElement('div', { ...css(styles.wrapper) }); + +const StyledComponent = withStyles(({ unit }) => ({ + wrapper: { borderRadius: unit }, +}))(Component); diff --git a/types/react-with-styles/tsconfig.json b/types/react-with-styles/tsconfig.json new file mode 100644 index 0000000000..fa3be8d7d3 --- /dev/null +++ b/types/react-with-styles/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "react-with-styles-tests.ts", + "lib/ThemedStyleSheet.d.ts" + ] +} diff --git a/types/react-with-styles/tslint.json b/types/react-with-styles/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/react-with-styles/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }