Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何编写以下代码的类型约束 接口IRouteProps{ 路径:字符串 名称:string } 常数路由配置:IRouteProps[]=[ { 路径:'/login', 名称:“登录” } ]; 让routename:任何; 常量路由:IRouteProps[]=routeConfig.forEach((路由:IRouteProps)=>{ routeNames[route.name]=route.path; });_Javascript_Typescript - Fatal编程技术网

Javascript 如何编写以下代码的类型约束 接口IRouteProps{ 路径:字符串 名称:string } 常数路由配置:IRouteProps[]=[ { 路径:'/login', 名称:“登录” } ]; 让routename:任何; 常量路由:IRouteProps[]=routeConfig.forEach((路由:IRouteProps)=>{ routeNames[route.name]=route.path; });

Javascript 如何编写以下代码的类型约束 接口IRouteProps{ 路径:字符串 名称:string } 常数路由配置:IRouteProps[]=[ { 路径:'/login', 名称:“登录” } ]; 让routename:任何; 常量路由:IRouteProps[]=routeConfig.forEach((路由:IRouteProps)=>{ routeNames[route.name]=route.path; });,javascript,typescript,Javascript,Typescript,我想从对象数组中获取每个对象的值,然后将该值用作另一个对象的键 如果您使用typescript来约束这个新对象的类型,您将如何编写它 如何编写“routeNames”的类型约束?您的类型不是很具体,因为名称和路径是字符串。因此,可以从中建模的结构是: type RoutesMap = Record<IRouteProps['name'], IRouteProps['path']> 但是我们将得到的类型只是mapstring:string。我们可以通过例如const关键字来更具体地描

我想从对象数组中获取每个对象的值,然后将该值用作另一个对象的键

如果您使用typescript来约束这个新对象的类型,您将如何编写它


如何编写“routeNames”的类型约束?

您的类型不是很具体,因为名称和路径是字符串。因此,可以从中建模的结构是:

type RoutesMap = Record<IRouteProps['name'], IRouteProps['path']>
但是我们将得到的类型只是map
string:string
。我们可以通过例如
const
关键字来更具体地描述。考虑:

const routesConfig = [
  {
    path: '/login',
    name: 'login'
  },
  {
    path: '/dashboard',
    name: 'dashboard'
  }
] as const; // pay attention here

type IRouteProps = typeof routesConfig[number];
type RoutesMap = Record<IRouteProps['name'], IRouteProps['path']>

const routes = routesConfig.reduce((routes, route: IRouteProps) => {
    routes[route.name] = route.path;
    return routes;
}, {} as RoutesMap);

const routeConfig=[
{
路径:'/login',
名称:“登录”
},
{
路径:'/dashboard',
名称:“仪表板”
}
]作为常量;//注意这里
类型IRouteProps=路由配置类型[编号];
输入routemap=Record
const routes=routeConfig.reduce((路由,路由:IRouteProps)=>{
routes[route.name]=route.path;
返回路线;
},{}作为路由映射);

在这种情况下,
RouteMap
被指定给RouteConfig对象中存在的值。

没有键入特定的值,只有值(字符串)的类型,所以
{[name:string]:string}
?我的初衷是想要一个代码提示。例如,在webstrom或vscode中,当调用“RouteName”时,它可以提示此对象中已有的键值对。所以我需要一个类型来约束这个变量。除非它们是硬编码的,而且它们看起来不像,因为
:string
s,这是不可能的,因为它们是动态的,由JS完成,在TS完成编译后非常感谢你,你提供的想法对我非常有用。
const routes = routesConfig.reduce((routes, route: IRouteProps) => {
    routes[route.name] = route.path;
    return routes;
}, {} as RoutesMap);
const routesConfig = [
  {
    path: '/login',
    name: 'login'
  },
  {
    path: '/dashboard',
    name: 'dashboard'
  }
] as const; // pay attention here

type IRouteProps = typeof routesConfig[number];
type RoutesMap = Record<IRouteProps['name'], IRouteProps['path']>

const routes = routesConfig.reduce((routes, route: IRouteProps) => {
    routes[route.name] = route.path;
    return routes;
}, {} as RoutesMap);