I have a few helper functions which are united into container object. And that functions can be called from any place at code.
const Utils = {
isSomeMethod: function (a: INodeType, b: INodeType) {
// Some logic
},
_nodeCheck: function (n: INodeType) {
// `this` : {
// node: INodeType,
// nn: INodeType,
// }
return (n !== this.node && Utils.isSomeMethod(n, this.nn));
},
...
}
That function can take arguments and also context variable this I have a question is there possibility to set any specific type to context variable.
NOTE: In example above method _nodeCheck have to take context variable with type {node: INodeType, nn: INodeType} (that 3rd party solution and I can't change it) also that method widely used in my code and I would like to have type checking.
You can specify the type of this as an extra parameter to the function. This parameter will not be emitted to JavaScript and will just be used for type checking.
const Utils = {
isSomeMethod: function (a: INodeType, b: INodeType) {
// Some logic
},
_nodeCheck: function (this: {node: INodeType, nn: INodeType}, n: INodeType) {
// This as the type specified above and is checked.
return (n !== this.node && Utils.isSomeMethod(n, this.nn));
},
...
}
let node!: INodeType;
Utils._nodeCheck(node) // not allowed this (aka Utils) is not assignable to {node: INodeType, nn: INodeType}
This is covered in the documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With