I have two interfaces;
interface A {
func: () => void;
}
interface B {
func: Function;
}
What's the difference between A
and B
interfaces?
CodePudding user response:
I think using Function
type isn’t very good idea, even TS linter warn you that this is dangerous because you don’t provide scheme of function:
Don't use Function
as a type. The Function
type accepts any function-like value.
It provides no type safety when calling the function, which can be a common source of bugs.
It also accepts things like class declarations, which will throw at runtime as they will not be called with new
.
If you are expecting the function to accept certain arguments, you should explicitly define the function shape.
So better use something like
interface A {
func: (a: string) => number;
}
Btw for void function you could also use VoidFunction
type instead of () => void;