Reactjs和Typescript-类型“EventTarget”上不存在属性“select”
这个类型检查问题花了我一整天的时间,我仍然没有解决它。我到处寻找,但我发现没有人经过。谁能帮我点个火吗?谢谢大家!Reactjs和Typescript-类型“EventTarget”上不存在属性“select”,reactjs,typescript,Reactjs,Typescript,这个类型检查问题花了我一整天的时间,我仍然没有解决它。我到处寻找,但我发现没有人经过。谁能帮我点个火吗?谢谢大家! <input type="text" value={shareURLValue} className={classnames("share-url-holder", { shared: Boolean(shareId) && !shareHidden, })}
<input
type="text"
value={shareURLValue}
className={classnames("share-url-holder", {
shared: Boolean(shareId) && !shareHidden,
})}
ref={saveShareURLRef}
onClick={e => {
e.target.select(); #Property 'select' does not exist on type 'EventTarget'.
}}
onKeyUp={e => {
e.target.select(); #Property 'select' does not exist on type 'EventTarget'.
}}
/>
您需要更明确地说明事件参数的类型:
onClick={(e: React.MouseEvent<HtmlInputElement>) => {
e.target.select(); #Property 'select' does not exist on type 'EventTarget'.
}}
及
您需要更明确地说明事件参数的类型:
onClick={(e: React.MouseEvent<HtmlInputElement>) => {
e.target.select(); #Property 'select' does not exist on type 'EventTarget'.
}}
及
使用e.currentTarget而不是e.target
从反应类型定义中:
interface SyntheticEvent<T> {
// other properties...
/**
* A reference to the element on which the event listener is registered.
*/
currentTarget: EventTarget & T;
// If you thought this should be `EventTarget & T`, see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12239
/**
* A reference to the element from which the event was originally dispatched.
* This might be a child element to the element on which the event listener is registered.
*
* @see currentTarget
*/
target: EventTarget;
}
换句话说,当target是MouseEvent时,它可能不是输入元素。使用currentTarget获取事件处理程序绑定到的元素。使用e.currentTarget而不是e.target
从反应类型定义中:
interface SyntheticEvent<T> {
// other properties...
/**
* A reference to the element on which the event listener is registered.
*/
currentTarget: EventTarget & T;
// If you thought this should be `EventTarget & T`, see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/12239
/**
* A reference to the element from which the event was originally dispatched.
* This might be a child element to the element on which the event listener is registered.
*
* @see currentTarget
*/
target: EventTarget;
}
换句话说,当target是MouseEvent时,它可能不是输入元素。使用currentTarget获取事件处理程序绑定到的元素。以下是我如何实现此功能的:
const handleSomeEvent = (e: SyntheticEvent<HTMLInputElement>) => {
/* now you have type-safe access to all the things */
e.currentTarget.[stuff]
}
return <input onClick={handleSomeEvent} onKeyUp={handleSomeEvent} {...otherProps}/>
以下是我如何让它工作的:
const handleSomeEvent = (e: SyntheticEvent<HTMLInputElement>) => {
/* now you have type-safe access to all the things */
e.currentTarget.[stuff]
}
return <input onClick={handleSomeEvent} onKeyUp={handleSomeEvent} {...otherProps}/>
看来你是对的,我会解决出现的其他问题,做测试,然后返回给你。结果出来了,我害怕这样做。我看到了其他的例子,因为在我看来,使用select是必要的。我们拭目以待。我感谢你的帮助!看来你是对的,我会解决出现的其他问题,做测试,然后返回给你。结果出来了,我害怕这样做。我看到了其他的例子,因为在我看来,使用select是必要的。我们拭目以待。我感谢你的帮助!我感谢你的帮助!但是你的提示给了我不同的检查顺序:[ts]找不到名称'HtmlInputElement'。[ts]命名空间“React”没有导出成员“KeyBoardEvent”。感谢您的帮助!但是你的提示给了我不同的检查顺序:[ts]找不到名称'HtmlInputElement'。[ts]命名空间“React”没有导出的成员“KeyBoardEvent”。