In this particular instance, the NameUnion
represents the type, while the nameArray
embodies an array of that specific type. It serves as a tool for the inclusion check you desire. By employing the doSomething
function, you can assess whether a parameter of type NameUnion
is present in the nameArray
.
This recommended strategy entails the following:
type NameUnion =
| 'email'
| 'newEmail'
| 'password'
| 'passwordConfirm'
| 'deleteAccountPassword';
const nameArray: NameUnion[] = ['email', 'newEmail', 'password', 'passwordConfirm', 'deleteAccountPassword'];
function doSomething(name: NameUnion): void {
if(nameArray.includes(name)){
// Implement actions here
console.log(`name is in the NameUnion: ${name}`);
} else {
console.log(`name is NOT in the NameUnion: ${name}`);
}
}
// Test Cases
doSomething('email'); // Expected output: "name is in the NameUnion: email"
doSomething('other'); // Expected output: "name is NOT in the NameUnion: other"