Turning a function signature into a type?
Turning a function signature into a type?
I have property that is a function and it is typed like this:
/**
* Function that performs the validation.
*/
validate: (vc: ValidationContext, object:any)=>boolean;
The same type signature is used in the class constructor:
constructor(validate: (vc: ValidationContext, object:any)=>boolean) {}
So if I change the signature in one place then I have to change it in another. Is it possible to define a type that encapsulates the type. Something like:
type ValidateType = (vc: ValidationContext, object:any)=>boolean
That way the validate
type can be updated in one place?
validate
Per the comments I got a little lucky in this question. This is how I implemented it (@cartant if you would to put this in an answer so you can get credit I'll delete it from here):
/**
* The type api signature for the validation function.
*/
type ValidationFunctionType = (vc: ValidationContext, object:any)=>boolean;
type ValidateType = (vc: ValidationContext, object: any) => boolean
interface ValidateType { (vc: ValidationContext, object: any) => boolean }
Really??? Wow got pretty lucky on that one! Thanks!
– Ole
2 days ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Yes, it's possible. You'd declare the type exactly as you've declared it in your question:
type ValidateType = (vc: ValidationContext, object: any) => boolean
. Or you could declare it using an interface:interface ValidateType { (vc: ValidationContext, object: any) => boolean }
.– cartant
2 days ago