Instead of intertwining code and types like the example below:
const compar8 : boolean | error = (action: string, n: number) => {
switch(action) {
case 'greater':
return n > 8;
case 'less':
return n < 8;
case 'equal':
return n === 8;
default:
throw new Error('Invalid action');
}
}
I prefer to have a setup similar to this:
// code.js
const compar8 = (action, n) => {
switch(action) {
case 'greater':
return n > 8;
case 'less':
return n < 8;
case 'equal':
return n === 8;
default:
throw new Error('Invalid action');
}
}
// types.ts
compar8 : (string, number) => boolean | error
The motivation behind this preference is twofold: 1. Improved readability and 2. Consistency in assigning specific types to functions. For example:
myType = (string, number) => boolean | error
This way, we can use the same type definition for all functions with identical signatures:
// types.ts
compar8: myType
compar9: myType
...
and so on
Is there a method to achieve this?