2017-11-29 1 views
0

유형으로 변환 할 수 없습니다. TypeScript를 처음 사용하며 다음 코드가 있습니다. React에 TypeScript 오류가있는 상태 비 저장 기능 : TS2352를 'Promise <StatelessComponent <{}>>'

import * as React from 'react'; 
const Product: React.SFC<{}> =() => <div>Product</div>; 
export default Product; 

import { asyncComponent } from 'react-async-component'; 

const AsyncProduct = asyncComponent({ 
    name: 'AsyncProduct', 
    serverMode: 'resolve', 
    resolve:() => { 
    return import(/* webpackChunkName: "Product" */ './Product') as Promise<React.SFC<{}>>; 
    }, 
}); 

export default AsyncProduct; 

./src/AsyncProduct.tsx

(7,12) : 오류 TS2352 : 유형 '약속' 이 '약속>'형식으로 변환 할 수 없습니다. 'typeof "/ 사용자/banyan/tmp/typescript-react-async-component-example/src/Product" 은'StatelessComponent < {}> '유형과 비교할 수 없습니다. 'typeof'/ Users/banyan/tmp/typescript-react-async-component-example/src/Product를 입력하십시오. " ' 은 서명과 일치하지 않습니다.'(소품 : {children ?: ReactNode; }, context ?: any) : ReactElement | 없는'. 나는 Promise<React.SFC<{}>Promise<any>로 변경하는 경우

, 그것은 컴파일 할 수 있지만 내가 어떻게 비 저장 기능을 지정할 수 있습니다?

yarn start로 재생하기위한 최소의 repo는 다음 유형의 https://github.com/banyan/typescript-react-async-component-example


정의는 다음과 같이이다 : node_modules/react-async-component/index.d.ts.

async/await 사용 :

/** 
* The configuration for an asynchronous component. 
*/ 
export interface Configuration<P> { 
    resolve:() => Promise<React.ComponentType<P>>; 
... 
} 
+0

귀하의 렌더링 기능이 아마 타에 필요 당신이 그들을 사용하지 않는 경우에도 반응 인수를? onst 제품 : React.SFC <{}> = (소품 : {어린이 : ReactNode;}, 컨텍스트 : : any) =>

Product
; 하지만 저는 추측하고 있습니다. 나는 아직 반응이 굉장하지 않다. :) –

답변

1

import 기능은 전체 모듈을 반환, 당신은 당신이 모듈 (기본 수출 또는 명명 된 하나)에서 사용할 정확히 무엇을 선택해야

const AsyncProduct = asyncComponent({ 
    name: 'AsyncProduct', 
    serverMode: 'resolve', 
    resolve: async() => { 
    var module = await import('./Product'); 
    return module.default; 
    }, 
}); 

또는 다음과 :

const AsyncProduct2 = asyncComponent({ 
    name: 'AsyncProduct', 
    serverMode: 'resolve', 
    resolve:() => { 
    return import('./Product').then(m=> m.default); 
    }, 
}); 
+1

나는 지금 이해했다! 고마워요 !! – banyan

관련 문제