虽然我的代码有效(下图),但是打字稿会抛出错误,说“属性'风格''在'HTMLElement'类型中不存在。”
正如你所看到的,我正在通过jQuery和amp;抓取元素元素的ID。
$(this.id).click(function () {
this.style.display = 'none';
});
但是这会导致以下错误:
`TS2339: Property 'style' does not exist on type 'HTMLElement'`.
我不确定处理这个ts错误的正确方法是什么。
有人会告诉我吗?
提前谢谢。
更多代码,因为它被请求了:
`/**
* Class represents a flash message
*/
export class FlashMessage {
flashMessageParentDivID:string;
iconID:string;
textID:string;
$: JQuery;
/**
*
* @param {string} flashMessageParentDiv - the string of the div's ID.
*/
constructor (flashMessageParentDiv: string, $: JQuery) {
this.flashMessageParentDivID = "#"+flashMessageParentDiv;
this.iconID = "#flash-message-icon",
this.textID = "#flash-message-text";
this.$ = $;
}
/**
* Displays the passed in message with the appropriate status.
* @param {string} message - the message we want to flash.
* @param {string} status: either 'error' or 'success'
*/
showWithMessage = function (message: string, status: string) {
//@todo: maybe throw an error if status does not equal error or success.
this.clearAndCreateFlashMessageInnerds();
this.$(this.flashMessageParentDivID).removeAttr("class");
this.$(this.flashMessageParentDivID).addClass(status);
this.$(this.iconID).removeAttr("class");
this.$(this.iconID).addClass("k-icon k-i-" + status);
this.$(this.textID).text(message);
this.$(this.flashMessageParentDivID).show();
this.$(this.flashMessageParentDivID).click(function () {
(<any>this).style.display = 'none';
});
};
...`
答案 0 :(得分:17)
在你的方法中使用它
(<HTMLElement>document.querySelector('#yourDomElmentID')).style.display = 'none';
我在我的代码中测试你的代码编译成功。
答案 1 :(得分:2)
Manohar Gavit建议,我需要进行类型转换。但是,他的建议并没有解决我的问题。由于某种原因,我的jQuery定义文件无法将import React from 'react';
import ReactDOM from 'react-dom';
import {
createStore
, applyMiddleware
} from 'redux'
import createSagaMiddleware from 'redux-saga'
import { Provider } from 'react-redux'
import MuiThemeProvider from 'material-ui/styles/MuiThemeProvider';
import darkBaseTheme from 'material-ui/styles/baseThemes/darkBaseTheme';
import getMuiTheme from 'material-ui/styles/getMuiTheme';
import './index.css';
import "font-awesome/css/font-awesome.css";
import MainComponent from './pages/main/main.component';
import reducers from './_redux';
import reduxSaga from './_reduxSaga';
import registerServiceWorker from './registerServiceWorker';
const sagaMiddleware = createSagaMiddleware();
let store = createStore(
reducers
, applyMiddleware( sagaMiddleware )
);
sagaMiddleware.run( reduxSaga );
const Container = () => (
// <MuiThemeProvider muiTheme={getMuiTheme(darkBaseTheme)} >
<Provider store={ store } >
<MuiThemeProvider>
<MainComponent />
</MuiThemeProvider>
</Provider>
);
ReactDOM.render(
<Container />
, document.getElementById( 'root' )
);
package.json:
{
"name": "my-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.16.2",
"font-awesome": "^4.7.0",
"history": "^4.7.2",
"material-ui": "^0.19.2",
"prop-types": "^15.6.0",
"react": "^15.6.1",
"react-bootstrap": "^0.31.3",
"react-dom": "^15.6.1",
"react-foundation": "^0.9.2",
"react-redux": "^5.0.6",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
"recharts": "^1.0.0-alpha.6",
"redux": "^3.7.2",
"redux-saga": "^0.15.6"
},
识别为具有HTMLElement
属性,但正如您在下面看到的那样...定义文件确实接受style
具有该属性。
any
答案 2 :(得分:0)
实际上,为了从TypeScript的类型安全中获得帮助,我认为最好的解决方案是创建自定义事件类型,因为我们只有很少的Event用例,我来举例说明。
interface IDom{
value?:string;
name?:string;
className?:string;
style?:any;
}
interface IEvent{
target?:IDom,
currentTarget?:IDom
}
const handleChange = (event:IEvent)=>{console.log(event?.target?.name)}
请注意,所有属性都保持为可选,因为否则我们不必要地会遇到打字错误,而如果我们不传递某些属性来定义事件侦听器,这将没有太大的作用!
答案 3 :(得分:0)
我在 forEach
中遇到过这个错误,NodeListOf<HTMLElement>
挽救了我的生命
document.querySelectorAll('.my-element').forEach((p: HTMLElement, i:number, arr:NodeListOf<HTMLElement>) => {
arr[arr.length - i].style.color = 'red';
})