You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I record some magical TS type problems here, and then study and analyze the causes. Some of them should be limitation of TS, you can refer to this issue.
The following are type errors and their current solutions:
1. Variables at root child of React.Fragment
✖️ Type error
constApp=sfc({Component(){return{title: 'hello'};},render: ({ data })=>(<>{data.title}</>// data.title is any)})
✔️ Type passed
// You can do either of the following:constApp=sfc({Component(){return{title: 'hello'};},render: ({ data })=>(<React.Fragment>{data.title}</React.Fragment>)})constApp2=sfc({Component(){return{title: 'hello'};},render: ({ data })=>(<div><>{data.title}</></div>)})
2. If styles is a function, you must use arrow functions to infer types correctly
3. In JSX(TSX is ok), static function must use the return statement to infer types correctly
✖️ Type error
constApp=sfc({Component({ utils,constant: {LAST_NAME}}){// utils and LAST_NAME are both any
...
},static: ()=>({constant: {LAST_NAME: 'sky'},utils: {connectName: (firstName,lastName)=>`${firstName}_${lastName}`}}),
...
});
✔️ Type passed
constApp=sfc({Component({ utils,constant: {LAST_NAME}}){
...
},static: ()=>{return{// need to explicitly use the return statementconstant: {LAST_NAME: 'sky'},utils: {connectName: (firstName,lastName)=>`${firstName}_${lastName}`}};},
...
})
4. In JSX(TSX is ok), when Component is arrow function, if there is any type in the function parameter during type inference, the whole data will be inferred as any
✖️ Type error
constApp=sfc({Component: ()=>{return{onChange: e=>console.log(e.target.value)};},render: ({ data })=>(<buttononClick={data.onChange}>test</button>// onChange is any)})
✔️ Type passed
constApp=sfc({Component(){// need to use the object property function syntaxreturn{onChange: e=>console.log(e.target.value)};},render: ({ data })=>(<buttononClick={data.onChange}>test</button>)})
5. The data parameter of the render function doesn‘t support direct deconstruction
✖️ Type error
constApp=sfc({Component(){return{title: 'hello'};},render: ({data: { title }})=>(<div>{title}</div>// title is any)})
✔️ Type passed
constApp=sfc({Component(){return{title: 'hello'};},render: ({ data })=>{const{ title }=data;return<div>{title}</div>;}})
This problem should be caused by the type circular reference problem of TS, there is no perfect solution for this problem. At present, my solution is to use extends to remove a layer of circular reference. For details, please refer to here.
I record some magical TS type problems here, and then study and analyze the causes. Some of them should be limitation of TS, you can refer to this issue.
The following are type errors and their current solutions:
1. Variables at root child of React.Fragment
✖️ Type error
✔️ Type passed
2. If styles is a function, you must use arrow functions to infer types correctly
✖️ Type error
✔️ Type passed
3. In JSX(TSX is ok), static function must use the return statement to infer types correctly
✖️ Type error
✔️ Type passed
4. In JSX(TSX is ok), when Component is arrow function, if there is any type in the function parameter during type inference, the whole data will be inferred as any
✖️ Type error
✔️ Type passed
5. The data parameter of the render function doesn‘t support direct deconstruction
✖️ Type error
✔️ Type passed
This problem should be caused by the type circular reference problem of TS, there is no perfect solution for this problem. At present, my solution is to use
extends
to remove a layer of circular reference. For details, please refer to here.6. Define functions in static function
✖️ Type error
✔️ Type passed
The text was updated successfully, but these errors were encountered: