由於陣營16現在允許custom DOM attributes,我想利用這個在我的打字稿代碼:屬性不上類型存在「DetailedHTMLProps,HTMLDivElement>」與之反應16
import * as React from 'react';
<div className="page" size="A4">
</div>
但收到此錯誤信息:
錯誤TS2339:屬性「大小」不存在於類型 '詳細HTML詳細信息< HTMLAttributes < HTMLDivElement>,HTMLDivElement>'。
這thread建議做一個module augmentation
,所以我想是這樣的:
import * as React from 'react';
declare module 'react' {
interface HTMLProps<T> {
size?:string;
}
}
同樣的錯誤消息。
最後,我也試圖聲明page
作爲一個新的HTML標籤:
declare global {
namespace JSX {
interface IntrinsicElements {
page: any
}
}
}
<page className="page" size="A4">
</page>
它擺脫了錯誤信息,但size
屬性完全被忽略在編譯的代碼,我結束了:
<page className="page">
</page>
理想情況下,最後一個是我的首選解決方案。我想使用size
自定義屬性以及page
自定義標籤。
tsconfig.js
{
"compilerOptions": {
"outDir": "build/dist",
"module": "esnext",
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": true,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "src",
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"suppressImplicitAnyIndexErrors": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": false,
"noUnusedParameters": false,
"allowUnusedLabels": true,
"allowUnreachableCode": true
}
}
我不知道很多關於打字稿,但一個想法-might是stupid-,怎麼樣努力'數據size'? – bennygenel
哇...它擺脫了我的錯誤,並編譯!是什麼讓你想到這個,爲什麼這個工作與諸如大小這樣的「非短跑」名字相反?你可以寫這個答案,以便我可以驗證嗎? –