JS, TS, Error and solution
よく指摘されるエラーと、回避させる方法をまとめました。
Uncaught SyntaxError: Unexpected token '.'
- xxx.yyyなどで参照するとき、xxxがnullだとerrorが出ます。
xxx && xxx.yyy
と一間開けるか、xxx?.yyy
で回避できます。
Uncaught TypeError: xxx.map is not a function.
- props.children.map(v=>v.key)などで参照するとき、childrenが配列でないとerrorが出ます
const getarr =a=>a instanceof Array?a:a?[]:[a]
が便利。- getarr(props.children).map(v=>v.key)と一間開けるか、
React.Children.map
を使います。
Cannot read property '1' of undefined
- 長さ1の配列
arr
にarr[1]
するとプロパティーがないといわれます。 arr.find((_,i) = >i===1) || null
を使うか、型を定義します。
Typescript error
JSX element 'T' has no corresponding closing tag.
- Typescriptの
<T>
がcomponentsとして認識されてしまいます。 - tsconfigを直すか、
<T>
を<T=unknown>
にします。
Argument of type 'any[]' is not assignable to parameter of type 'ConcatArray<never>'.
- 型がない場合は、
.concat(...(arr as never[])
を通します。