์ด๊ฒ์ 3.5.2์์ ์๋ํ์ง๋ง 3.6.2์์๋ ์คํจํฉ๋๋ค.
strict
๋ชจ๋๊ฐ ๋นํ์ฑํ๋์์ต๋๋ค.
ํ์ดํ์ด๋ TypeScript์ ๋ฒ๊ทธ์ ๋๊น?
์ฌ๊ธฐ์ ์คํจํ ํ ์คํธ๋ฅผ ์ถ๊ฐํ์ต๋๋ค : https://github.com/DefinitelyTyped/DefinitelyTyped/commit/f036856dfc9a7fd140f378402c102206738ec0a4.
import * as React from 'react';
// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5db94ca7fd3570dc23588b404d7bb669a7886a8a/types/react-router/index.d.ts#L146
declare function withRouter<P, C extends React.ComponentType<P>>(
component: C & React.ComponentType<P>,
): unknown;
declare const Component: React.ComponentType<{}>;
/*
Argument of type 'ComponentType<{}>' is not assignable to parameter of type 'ComponentClass<{}, any> | (ComponentClass<{}, any> & FunctionComponent<{}>)'.
Type 'FunctionComponent<{}>' is not assignable to type 'ComponentClass<{}, any> | (ComponentClass<{}, any> & FunctionComponent<{}>)'.
Type 'FunctionComponent<{}>' is not assignable to type 'ComponentClass<{}, any> & FunctionComponent<{}>'.
Type 'FunctionComponent<{}>' is not assignable to type 'ComponentClass<{}, any>'.
Type 'FunctionComponent<{}>' provides no match for the signature 'new (props: {}, context?: any): Component<{}, any, any>'.
*/
withRouter(Component);
๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ์๊ณ ์๋ค๋ฉด ๋์ ํ ์์ฒญ์ํ์ญ์์ค.
@types/xxxx
ํจํค์ง๋ฅผ ์ฌ์ฉํด ๋ณด์๋๋ฐ ๋ฌธ์ ๊ฐ์์์ต๋๋ค.Definitions by:
์ index.d.ts
) ์์ฐฝ ์ฑ๊ฐ.์ ์๋ฅผ ์ธ๊ธํ์ง ์์ผ๋ฉด ๋ฌธ์ ๊ฐ ๋ฌด์๋ฉ๋๋ค.
๋๋ ์ด๊ฒ์ ํํค์น ์๊ฐ์ด ์์ ๊ฒ์
๋๋ค. ComponentType
์ฌ์ฉ์ ์ ๊ฑฐํ๊ณ ref
๋ฐ children
๊ณผ ๊ด๋ จํ์ฌ ๋ช
์ ์ props์ ํจ๊ป ์ผ๋ฐ ํจ์๋ฅผ ์ฌ์ฉํด์ผํ๋ค๊ณ ์๊ฐํฉ๋๋ค. ComponentType
๋ ๋
ธ์กฐ๋ก ์ธํด ํญ์ ์ฝ๊ฐ ๋ฌธ์ ๊ฐ์์์ต๋๋ค. ์ค๋ฅ๋ฅผ ์์ฑํ์ง ์์ผ๋ฉด ๋ค๋ฅธ hoc์ ๋งค์ฐ ํฐ ๊ฒฐํฉ์ ์์ฑํ์ต๋๋ค. ๋์๊ด ์๋น์๋ ์ด์ฐจํผ ์ ์ ์ ๋ง์ ธ์๋ ์๋๋ฏ๋ก ์ด๋ฌํ ์ ๋ณด๋ฅผ ์์ด ๋ฒ๋ฆฌ๋ ๊ฒ์ ๋ฌธ์ ๊ฐ๋์ง ์์ต๋๋ค.
๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. react-router repo (https://github.com/ReactTraining/react-router/issues/6906)์ ์ด๋ฏธ๋ณด๊ณ ๋์์ง๋ง ์ฌ๊ธฐ๋ก ๋ฆฌ๋๋ ์ ๋์์ต๋๋ค.
์ด ์์ ํ์๋ ์ฌ์ ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ค๋ฅ ๋ฉ์์ง๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Argument of type '({ myProp, history }: RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }) => Element' is not assignable to parameter of type 'ComponentType<RouteComponentProps<{}, StaticContext, any>>'.
Type '({ myProp, history }: RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }) => Element' is not assignable to type 'FunctionComponent<RouteComponentProps<{}, StaticContext, any>>'.
Types of parameters '__0' and 'props' are incompatible.
Type 'PropsWithChildren<RouteComponentProps<{}, StaticContext, any>>' is not assignable to type 'RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }'.
Property 'myProp' is missing in type 'RouteComponentProps<{}, StaticContext, any> & { children?: ReactNode; }' but required in type '{ myProp: boolean; }'.
๋ค์๊ณผ ๊ฐ์ด ์ ์ ๋ ๊ตฌ์ฑ ์์
import { RouteComponentProps, withRouter } from "react-router-dom";
interface Props extends RouteComponentProps {
myProp: boolean;
}
const Component = ({ myProp, match }: Props) => null
export default withRouter(Component);
๋๋ ์ฌ์ฉํ๊ณ ์๋ค
typescript: 3.6.3
@types/react-router: 5.0.4
@ karol-majewski ์ ๋ TS 3.6.3์ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ ์ต์ ๋ฒ์ @types/react-router: 5.0.4
์ด ์ค๋จ๋๊ณ ์ด์ ๋ฒ์ ์ผ๋ก ๋์๊ฐ๋ ๊ฒ์ ๊ด์ฐฎ์ต๋๋ค.
export function withRouter<P extends RouteComponentProps<any>, C extends React.ComponentType<P>>(
component: C & React.ComponentType<P>,
): React.ComponentClass<Omit<P, keyof RouteComponentProps<any>> & WithRouterProps<C>> & WithRouterStatics<C>;
์ ๋ ๋ชจ๋๊ฐ ์๋ก์ด ๋ฌธ์ ๋ฅผ ์ด๋๋ก ๊ถ์ฅํฉ๋๋ค. ํน์ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ฒฝ์ฐ ๋น๊ณต๊ฐ ์ค๋ ๋์์๋ง ๋๊ธ์ ๋ฌ๋ฉด ์๋ฆผ์๋ฐ์ ์ ์์ต๋๋ค. ์ด์ typescript ํ๋ ์ด ๊ทธ๋ผ์ด๋์์ ts ๋ฒ์ + ๊ตฌ์ฑ์ ๊ตฌ์ฑ ํ ์ ์์ผ๋ฉฐ ์ ํ ์ ์๋ฅผ ๋ค์ด๋ก๋ ํ ์ ์์ต๋๋ค. ์๋ก์ด ์ด์์ ๋งํฌ๋ฅผ ํฌํจํ๋ฉด ๊ด๋ฆฌ์์๊ฒ ๋ง์ ๋์์ด๋ฉ๋๋ค. ๊ฐ์ฌํฉ๋๋ค :
์ด๊ฒ์ strictFunctionTypes
๋ก๋ง ๋์ง๋ ๊ฒ์ฒ๋ผ ๋ณด์ด๋ฉฐ ๋ง์์ ํด๊ฒฐ์ฑ
์ด ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ด ์๋๋์ง ๋ณผ ๊ฒ์
๋๋ค.
@sandersn ์์ ์ฌํญ ์ ๋ ๋๋ฆฐ ํ ๋ค์ ์ด ์ ์์ต๋๊น?
IIRC, @ eps1lon ์ ์ด๊ฒ์ด TypeScript ๋ฒ๊ทธ ๋๋ฌธ https://github.com/microsoft/TypeScript/issues/33490
์์ ์ฌํญ : https://github.com/microsoft/TypeScript/pull/34607.
์ผ๊ฐ ๋ฒ์ ์์ ์ ์๋ํฉ๋๋ค. ๋์ดํฐ ๋งํฌ
ํ์ดํ ์คํฌ๋ฆฝํธ๋ ์๊ฐ ๋ญ๋น์ ๋๋ค. ์ ๋์ด ๋ฉ์ฒญํ ์ค๋ฅ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ช ์๊ฐ์ ๋ณด๋์ต๋๋ค. ๋ฐ๋ผ์ ํ์ดํ ์คํฌ๋ฆฝํธ๊ฐ ์ถ์ ํธํ๊ฒํ๋๋ก ๋ง๋ค์ด ์ก๋ค๋ฉด ๋ถ๋ช ํ ์์ ์ ์ํํ์ง ์๊ณ ์ค๋ฅ์ ๋ฒ๊ทธ๋ก โโ๊ฐ๋ ์ฐจ ์์ต๋๋ค.
์ด ๋ฌธ์ ๋ TS 3.7์์ ์์ ๋์์ต๋๋ค. ํ์.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ์์ ํ์๋ ์ฌ์ ํ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ค๋ฅ ๋ฉ์์ง๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
๋ค์๊ณผ ๊ฐ์ด ์ ์ ๋ ๊ตฌ์ฑ ์์
๋๋ ์ฌ์ฉํ๊ณ ์๋ค