Home > OS >  How to get the type of a list of values from an array?
How to get the type of a list of values from an array?

Time:11-15

I have this type:

type RouteList = 'admin/dashboard' | 'admin/users' | 'admin/roles';

How can I create this type above from this array dynamically?

const routeList = ['admin/dashboard','admin/users','admin/roles'];

CodePudding user response:

You can use this code :

const routeList = ['admin/dashboard','admin/users','admin/roles'] as const;

type RouteList = typeof routeList[number]

CodePudding user response:

try this code, u can use array includes().

const yourRoute = 'admin/users';
const routeList = ['admin/dashboard','admin/users','admin/roles'];
let isApproved = false;

if (routeList.includes(yourRoute)) {
    isApproved = true;
}

console.log(isApproved);
  • Related