Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion types/styled-components/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Type definitions for styled-components 5.0
// Type definitions for styled-components 5.1
// Project: https://github.com/styled-components/styled-components, https://styled-components.com
// Definitions by: Igor Oleinikov <https://github.com/Igorbek>
// Ihor Chulinda <https://github.com/Igmat>
Expand All @@ -10,6 +10,7 @@
// Matthew Wagerfield <https://github.com/wagerfield>
// Yuki Ito <https://github.com/Lazyuki>
// Maciej Goszczycki <https://github.com/mgoszcz2>
// Danilo Fuchs <https://github.com/danilofuchs>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.9

Expand Down Expand Up @@ -144,6 +145,12 @@ type ForwardRefExoticBase<P> = Pick<
keyof React.ForwardRefExoticComponent<any>
>;

// Config to be used with withConfig
export interface StyledConfig<O extends object = {}> {
// TODO: Add all types from the original StyledComponentWrapperProperties
shouldForwardProp?: (prop: keyof O, defaultValidatorFn: (prop: keyof O) => boolean) => boolean;
}

// extracts React defaultProps
type ReactDefaultProps<C> = C extends { defaultProps: infer D; } ? D : never;

Expand Down Expand Up @@ -258,6 +265,8 @@ export interface ThemedStyledFunction<
>(
attrs: Attrs<StyledComponentPropsWithRef<C> & U, NewA, T>
): ThemedStyledFunction<C, T, O & NewA, A | keyof NewA>;

withConfig: <Props extends O = O>(config: StyledConfig<StyledComponentPropsWithRef<C> & Props>) => ThemedStyledFunction<C, T, Props, A>;
}

export type StyledFunction<
Expand Down
6 changes: 5 additions & 1 deletion types/styled-components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
},
"types": "index",
"typesVersions": {
">=3.7.0-0": { "*": ["ts3.7/*"] }
">=3.7.0-0": {
"*": [
"ts3.7/*"
]
}
}
}
59 changes: 59 additions & 0 deletions types/styled-components/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,65 @@ const AttrsWithOnlyNewProps = styled.h2.attrs({ as: "h1" })`

const AttrsInputExtra = styled(AttrsInput).attrs({ autoComplete: "off" })``;
<AttrsInputExtra />;
/**
* withConfig
*/

/**
* shouldForwardProp
*/

// $ExpectError
const WithConfig = styled("div").withConfig()`
color: red;
`;

styled("div").withConfig({})`
color: red;
`;

styled("div").withConfig<{ myProp: boolean }>({
shouldForwardProp: (prop, defaultValidatorFn) => prop === "myProp",
})<{ otherProp: string }>`
color: red;
${p => {
// $ExpectType boolean
p.myProp;
return css``;
}}
${p => {
// $ExpectType string
p.otherProp;
return css``;
}}
`;

styled("input").withConfig({
shouldForwardProp: (prop) => prop === "disabled",
})`
color: red;
`;

styled('div').withConfig({
shouldForwardProp: (prop, defaultValidatorFn) => ['filterThis'].indexOf(prop) !== -1,
})`
color: red;
`;

styled('div').withConfig({
shouldForwardProp: (prop, defaultValidatorFn) => defaultValidatorFn(prop),
})`
color: red;
`;

styled("div").withConfig<{ test: boolean }>({
// $ExpectError
shouldForwardProp: (prop, defaultValidatorFn) => prop === "invalidProp" && true,
})`
color: red;
${p => p.test && css``}
${p => p.invalidProp && css``} // $ExpectError
`;

/**
* component type
Expand Down
8 changes: 8 additions & 0 deletions types/styled-components/ts3.7/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ type ForwardRefExoticBase<P> = Pick<
keyof React.ForwardRefExoticComponent<any>
>;

// Config to be used with withConfig
export interface StyledConfig<O extends object = {}> {
// TODO: Add all types from the original StyledComponentWrapperProperties
shouldForwardProp?: (prop: keyof O, defaultValidatorFn: ((prop: keyof O) => boolean)) => boolean;
}

// extracts React defaultProps
type ReactDefaultProps<C> = C extends { defaultProps: infer D; } ? D : never;

Expand Down Expand Up @@ -224,6 +230,8 @@ export interface ThemedStyledFunction<
>(
attrs: Attrs<StyledComponentPropsWithRef<C> & U, NewA, T>
): ThemedStyledFunction<C, T, O & NewA, A | keyof NewA>;

withConfig: <Props extends O = O>(config: StyledConfig<StyledComponentPropsWithRef<C> & Props>) => ThemedStyledFunction<C, T, Props, A>;
}

export type StyledFunction<
Expand Down
60 changes: 60 additions & 0 deletions types/styled-components/ts3.7/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,66 @@ const AttrsWithOnlyNewProps = styled.h2.attrs({ as: "h1" })`
const AttrsInputExtra = styled(AttrsInput).attrs({ autoComplete: "off" })``;
<AttrsInputExtra />;

/**
* withConfig
*/

/**
* shouldForwardProp
*/

// $ExpectError
const WithConfig = styled("div").withConfig()`
color: red;
`;

styled("div").withConfig({})`
color: red;
`;

styled("div").withConfig<{ myProp: boolean }>({
shouldForwardProp: (prop, defaultValidatorFn) => prop === "myProp",
})<{ otherProp: string }>`
color: red;
${p => {
// $ExpectType boolean
p.myProp;
return css``;
}}
${p => {
// $ExpectType string
p.otherProp;
return css``;
}}
`;

styled("input").withConfig({
shouldForwardProp: (prop) => prop === "disabled",
})`
color: red;
`;

styled('div').withConfig({
shouldForwardProp: (prop, defaultValidatorFn) => ['filterThis'].indexOf(prop) !== -1,
})`
color: red;
`;

styled('div').withConfig({
shouldForwardProp: (prop, defaultValidatorFn) => defaultValidatorFn(prop),
})`
color: red;
`;

styled("div").withConfig<{ test: boolean }>({
// $ExpectError
shouldForwardProp: (prop, defaultValidatorFn) => prop === "invalidProp" && true,
})`
color: red;
${p => p.test && css``}
${p => p.invalidProp && css``} // $ExpectError
`;

/**
* component type
*/
Expand Down