오류 : 유형에 호출 서명이없는 식을 호출 할 수 없습니다.
저는 타이프 스크립트를 처음 접했고 두 개의 클래스가 있습니다. 부모 클래스에는 다음이 있습니다.
abstract class Component {
public deps: any = {};
public props: any = {};
public setProp(prop: string): any {
return <T>(val: T): T => {
this.props[prop] = val;
return val;
};
}
}
자식 클래스에는 다음이 있습니다.
class Post extends Component {
public toggleBody: string;
constructor() {
this.toggleBody = this.setProp('showFullBody');
}
public showMore(): boolean {
return this.toggleBody(true);
}
public showLess(): boolean {
return this.toggleBody(false);
}
}
showMore 및 ShowLess 모두 "형식에 호출 서명이없는 식을 호출 할 수 없습니다."라는 오류를 표시합니다.
그러나 setProp이 반환하는 함수에는 호출 서명이 있습니까? 함수의 타이핑에 대해 중요한 것을 오해하고 있다고 생각하지만 그것이 무엇인지 모르겠습니다.
감사!
반환하는 함수에는 호출 서명이 있지만 Typescript에 : any
서명 을 추가하여 완전히 무시하도록 지시 했습니다.
그러지 마.
"유형에 호출 서명이없는 표현식을 호출 할 수 없습니다."
귀하의 코드에서 :
class Post extends Component {
public toggleBody: string;
constructor() {
this.toggleBody = this.setProp('showFullBody');
}
public showMore(): boolean {
return this.toggleBody(true);
}
public showLess(): boolean {
return this.toggleBody(false);
}
}
당신은 public toggleBody: string;
. 를 string
함수로 호출 할 수 없습니다 . 따라서 : this.toggleBody(true);
및this.toggleBody(false);
Let's break this down:
The error says
Cannot invoke an expression whose type lacks a call signature.
The code:
The problem is in this line public toggleBody: string;
&
it's relation to these lines:
...
return this.toggleBody(true);
...
return this.toggleBody(false);
- The result:
Your saying toggleBody
is a string
but then your treating it like something that has a call signature
(i.e. the structure of something that can be called: lambdas, proc, functions, methods, etc. In JS just function tho.). You need to change the declaration to be public toggleBody: (arg: boolean) => boolean;
.
Extra Details:
"invoke" means your calling or applying a function.
"an expression" in Javascript is basically something that produces a value, so this.toggleBody()
counts as an expression.
"type" is declared on this line public toggleBody: string
"lacks a call signature" this is because your trying to call something this.toggleBody()
that doesn't have signature(i.e. the structure of something that can be called: lambdas, proc, functions, methods, etc.) that can be called. You said this.toggleBody
is something that acts like a string.
In other words the error is saying
Cannot call an expression (this.toggleBody) because it's type (:string) lacks a call signature (bc it has a string signature.)
I think what you want is:
abstract class Component {
public deps: any = {};
public props: any = {};
public makePropSetter<T>(prop: string): (val: T) => T {
return function(val) {
this.props[prop] = val
return val
}
}
}
class Post extends Component {
public toggleBody: (val: boolean) => boolean;
constructor () {
super()
this.toggleBody = this.makePropSetter<boolean>('showFullBody')
}
showMore (): boolean {
return this.toggleBody(true)
}
showLess (): boolean {
return this.toggleBody(false)
}
}
The important change is in setProp
(i.e., makePropSetter
in the new code). What you're really doing there is to say: this is a function, which provided with a property name, will return a function which allows you to change that property.
The <T>
on makePropSetter
allows you to lock that function in to a specific type. The <boolean>
in the subclass's constructor is actually optional. Since you're assigning to toggleBody
, and that already has the type fully specified, the TS compiler will be able to work it out on its own.
Then, in your subclass, you call that function, and the return type is now properly understood to be a function with a specific signature. Naturally, you'll need to have toggleBody
respect that same signature.
It means you're trying to call something that isn't a function
const foo = 'string'
foo() // error
Add a type to your variable and then return.
Eg:
const myVariable : string [] = ['hello', 'there'];
const result = myVaraible.map(x=> {
return
{
x.id
}
});
=> Important part is adding the string[] type etc:
I had the same error message. In my case I had inadvertently mixed the ES6 export default function myFunc
syntax with const myFunc = require('./myFunc');
.
Using module.exports = myFunc;
instead solved the issue.
'Programming' 카테고리의 다른 글
Reactjs에서 {… this.props}의 의미는 무엇입니까? (0) | 2020.08.21 |
---|---|
Webpack 및 Babel을 사용하여 "이 파일 유형을 처리하려면 적절한 로더가 필요할 수 있습니다." (0) | 2020.08.20 |
JavaDoc에서 @see 사용? (0) | 2020.08.20 |
Python으로 오디오 재생 (0) | 2020.08.20 |
저장소 변경에 대한 이메일 알림을 보내는 Git 후크 (0) | 2020.08.20 |