반응형
오른쪽 유형 스크립트 유형:Svelte에서 핸들러 변경
다음 코드가 있습니다.
<select class="form-control" on:change={pathChanged}>
의 서명은 다음과 같습니다.
function pathChanged(event: { target: HTMLSelectElement }) {
를 사용하여 실행 시 다음 오류가 발생합니다.
Error: Type '(event: { target: HTMLSelectElement; }) => void' is not assignable to type 'FormEventHandler<HTMLSelectElement>'.
Types of parameters 'event' and 'event' are incompatible.
Type 'Event & { currentTarget: EventTarget & HTMLSelectElement; }' is not assignable to type '{ target: HTMLSelectElement; }'.
Types of property 'target' are incompatible.
Type 'EventTarget | null' is not assignable to type 'HTMLSelectElement'.
Type 'null' is not assignable to type 'HTMLSelectElement'. (ts)
<select class="form-control" on:change={pathChanged}>
어떤 서명이 있어야 합니까?
그 사건은 당신이 원하는 것보다 덜 구체적이다. 이 시나리오에서는 일반적으로 함수 내에서 형식 어설션을 사용하여 이 문제를 해결합니다.
function pathChanged(event: Event) {
const target = event.target as HTMLSelectElement;
// ...
}
오류에 올바르게 입력해야 한다고 명시되어 있지만, 이를 사용하는 것도 효과적일 것입니다.
function pathChanged(event: { currentTarget: HTMLSelectElement })
입력 유형으로 이 작업을 수행했습니다.
// added this interface
interface FormEventHandler<T> {
target: EventTarget | null;
}
// then in the function
const onChangeFile = async (event: FormEventHandler<HTMLInputElement>) => {
const target = event.target as HTMLInputElement;
// your code
}
반응형
'개발하자' 카테고리의 다른 글
파이썬에서 잘못된 경로 문자를 제거하는 방법은 무엇입니까? (0) | 2022.11.06 |
---|---|
미래의 인스턴스 <String?문자열 데이터 플러터/파이어베이스 대신 >' (0) | 2022.11.04 |
Flater - 데이터가 변경될 때 getx 컨트롤러가 업데이트되지 않음 (0) | 2022.11.03 |
테라폼의 Azure App Service 고정 슬롯 설정 (0) | 2022.11.02 |
react/typescript: 매개 변수 'props'에 암시적으로 'any' 유형 오류가 있습니다. (0) | 2022.11.01 |