There is a constant declaration mentioned below:
export const Actions = {
VIEW: 'view',
EDIT: 'edit',
};
Imagine there's a function like this:
// Ensuring that the action variable below is always a string with value either view or edit
function insertAction(action: string): void {
console.log('I have inserted the action successfully');
}
The objective is to restrict the action parameter to only have values of view
| edit
, and this restriction should be dynamic. Please refrain from suggesting a union solution, as it does not serve the purpose.
I attempted using a signature like this:
function insertAction(action: typeof Actions[keyof typeof Actions]
However, it did not work as expected.
Even after defining the signature, I am able to call the function insertAction('whatever')
without any compilation errors or linter warnings.
I am using TypeScript version 4.4.2 in my package.json, along with PHPStorm Editor and Next.js
"typescript": "^4.4.2"