2017-11-30 5 views
0

TypeScript에서 컴파일 할 React.createElement를 가져올 수 없습니다.TypeScript/JSX의 React.createElement에 대한 올바른 구문

interface IColorPickerProps { 

} 

interface IColorPickerState { 

} 

class ColorPicker extends React.Component<IColorPickerProps, IColorPickerState> { 
    constructor(props: IColorPickerProps) { 
     super(props); 
    } 
} 

구성 요소 만들기 : 내가 생성자를 제거하면

Argument of type 'typeof ColorPicker' is not assignable to parameter of type 'string | ComponentClass<IColorPickerProps> | StatelessComponent<IColorPickerProps>'. 
    Type 'typeof ColorPicker' is not assignable to type 'StatelessComponent<IColorPickerProps>'. 
    Type 'typeof ColorPicker' provides no match for the signature '(props: IColorPickerProps & { children?: ReactNode; }, context?: any): ReactElement<any> | null'. 

이 오류가 사라집니다 :

let props: any = {} 
React.createElement(ColorPicker, props) 

나는이 컴파일 오류가 발생합니다. 소품을 동적으로 전달해야하므로 구문을 사용할 수 없습니다. 어떤 아이디어?

+0

'React.createElement (ColorPicker, props)'는 왜' '일까요? –

+0

Props는 ColorPicker를 만드는 클래스에 매개 변수로 동적으로 전달되기 때문에. '''React.createElement (ColorPicker, props)'는 컴파일 타임 검사를하지 못합니다. – sixtstorm1

답변

0

React의 최신 버전과 유형 정의의 최신 버전으로 업그레이드하여이 문제를 해결했습니다. 이것은 내가 사용하고있는 유형 정의에 문제가있는 것 같습니다. 여러분 모두를 도와 주셔서 감사합니다.

1

filename.tsx를 실행 한 다음 node filename.js를 실행하면 나에게 맞는 코드를 컴파일 할 수 있습니다. 이 코드와 함께 가지고있는 다른 코드가 있습니까?

클래스의 소품을 입력하는 요점은 ColorPicker 클래스로 전달 될 내용을 알고 있다는 것입니다. 필자의 의견으로는 IColorPickerProps 인터페이스가 이와 같이 전달 될 소품을 포함하도록 수정하는 것이 가장 좋습니다.

interface IColorPickerProps { 
    name: string, 
    age: number 
} 

그리고

let myprops: IColorPickerProps = { 
    name: ... 
    age: ... 
} 

는 다음 종류의 타입 검사의 목적을 격파하고있는 같은 소품을 입력하는 경우.

+0

맞습니다. 컴파일됩니다. 나는 VS 코드를 사용하고 있으며 TypeScript 검사를 통해 오류가 발생하지만 괜찮습니다. 왜 그런가? 타입 검사는 어떤 프롭이 수신되는지를 모르는 특별한 팩토리 클래스이며, 프롭은 동적으로 전달되어야한다. – sixtstorm1

+0

나는 소품을 받고 있다는 의미에서 약간 혼란 스럽다. 보내지는 소품 수가 다른가요, 아니면 매번 소품 종류가 다른가요? – becks

관련 문제