Skip to content
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

Add ComponentType overloading #737

Merged
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
6 changes: 3 additions & 3 deletions packages/create-emotion-styled/types/react.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.3

import { ComponentClass, Ref, SFC } from 'react';
import { ComponentType, ComponentClass, Ref, SFC } from 'react';
import { ClassInterpolation } from 'create-emotion';

import {
Expand All @@ -24,7 +24,7 @@ export interface StyledComponentMethods<Props extends object, InnerProps extends
): StyledStatelessComponent<Props, IP, Theme>;

withComponent<IP extends object>(
component: ComponentClass<IP>,
component: ComponentClass<IP> | ComponentType<IP>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just looking at the react types and ComponentType is defined like this

type ComponentType<P = {}> = ComponentClass<P> | StatelessComponent<P>;

So couldn't ComponentClass be removed here and the other places which are ComponentClass<IP> | ComponentType<IP>?

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/a5b0278214093cc8714871d1db74bb53ed0b7e76/types/react/index.d.ts#L51

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mitchellhamilton It is also same in Preact, but I thought this is more clear, since this ComponentType<IP> is only for the library code who returns ComponentType<IP> in their code.

options?: StyledOptions,
): StyledOtherComponent<Props, IP, Theme>;
}
Expand Down Expand Up @@ -68,7 +68,7 @@ export interface CreateStyledFunction<Theme extends object> {
): CreateStyledStatelessComponent<IP, Theme>;

<IP extends object>(
component: ComponentClass<IP>,
component: ComponentClass<IP> | ComponentType<IP>,
options?: StyledOptions,
): CreateStyledOtherComponent<IP, Theme>;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/create-emotion-styled/types/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,27 @@ const CSSPropComp = createStyled.div();
color: blue;
`}
/>;

interface TestComponentTypeProps {
value: number;
}

declare const TestComponentType: React.ComponentType<TestComponentTypeProps>;

const StyledComponentType0 = createStyled(TestComponentType)({
color: 'red',
});

const StyledComponentType1 = createStyled(TestComponentType)`
color: red;
`;

const ComposingCompType = createStyled.div`
${StyledComponentType1} {
background-color: green;
}
`;

<StyledComponentType0 value={5} />;
<StyledComponentType1 value={4} />;
<ComposingCompType />;
6 changes: 3 additions & 3 deletions packages/preact-emotion/types/preact.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Definitions by: Junyoung Clare Jang <https://github.com/Ailrun>
// TypeScript Version: 2.6

import { ComponentConstructor, FunctionalComponent, Ref } from 'preact';
import { ComponentConstructor, ComponentFactory, FunctionalComponent, Ref } from 'preact';
import { ClassInterpolation } from 'create-emotion';
import {
Interpolation,
Expand All @@ -23,7 +23,7 @@ export interface StyledComponentMethods<Props extends object, InnerProps extends
): StyledStatelessComponent<Props, IP, Theme>;

withComponent<IP extends object>(
component: ComponentConstructor<IP>,
component: ComponentConstructor<IP> | ComponentFactory<IP>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the previous comment, couldn't this usage and the other just be ComponentFactory<IP>?

https://github.com/developit/preact/blob/master/src/preact.d.ts#L40

options?: StyledOptions,
): StyledOtherComponent<Props, IP, Theme>;
}
Expand Down Expand Up @@ -67,7 +67,7 @@ export interface CreateStyledFunction<Theme extends object> {
): CreateStyledStatelessComponent<IP, Theme>;

<IP extends object>(
component: ComponentConstructor<IP>,
component: ComponentConstructor<IP> | ComponentFactory<IP>,
options?: StyledOptions,
): CreateStyledOtherComponent<IP, Theme>;
}
Expand Down
24 changes: 24 additions & 0 deletions packages/preact-emotion/types/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,27 @@ const Parent = styled.div`
color: blue;
}
`;

interface TestComponentFactoryProps {
value: number;
}

declare const TestComponentFactory: Preact.ComponentFactory<TestComponentFactoryProps>;

const StyledComponentFactory0 = styled(TestComponentFactory)({
color: 'red',
});

const StyledComponentFactory1 = styled(TestComponentFactory)`
color: red;
`;

const ComposingCompFactory = styled.div`
${StyledComponentFactory1} {
background-color: green;
}
`;

<StyledComponentFactory0 value={5} />;
<StyledComponentFactory1 value={4} />;
<ComposingCompFactory />;