私のアプリケーション(npm 5.4.2、反応15.4、typescript 2.5.3、webpack 2.2.1、webpack-dev-server 2.4.1)で次のエラーが発生します。
これは動作します:
<div style={{position: "absolute"}}>working</div>
これはコンパイルされません:
const mystyle = {
position: "absolute"
}
<div style={mystyle}>not working</div>
コンパイルエラーは次のとおりです。
ERROR in ./src/components/Resource.tsx
(61,18): error TS2322: Type "{ style: { position: string; }; children: string; }" is not assignable to type "DetailedHTMLProps<HTMLAttributes<HTMLDivElement>, HTMLDivElement>".
Type "{ style: { position: string; }; children: string; }" is not assignable to type "HTMLAttributes<HTMLDivElement>".
Types of property "style" are incompatible.
Type "{ position: string; }" is not assignable to type "CSSProperties".
Types of property "position" are incompatible.
Type "string" is not assignable to type ""inherit" | "initial" | "unset" | "fixed" | "absolute" | "static" | "relative" | "sticky"".
webpack: Failed to compile.
しかし、違いは何ですか? 私はそれを修正することができます:
const mystyle = {
position: "absolute" as "absolute"
}
これは良い解決策ですか?
私は他のスタイル/ CSSのプロパティにこの問題がありません。
githubで同様の問題が見つかりました: https://github.com/Microsoft/TypeScript/issues/11465 それを正しく理解していれば、それはより高級なバージョンのタイスクリプト・バグでした。
どんな助けもありがたい。
回答:
回答№1については9これは回避策ですが、問題ありません。 その他の解決策は次のとおりです。
const mystyles = {
position: "absolute",
} as React.CSSProperties;
あなたはこれを確認することができます 問題 解決されます。 TS 2.6の周りには、マイルストーンで判断すると思います。