Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/368.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 TypeScript中的嵌套对象_Javascript_Typescript - Fatal编程技术网

Javascript TypeScript中的嵌套对象

Javascript TypeScript中的嵌套对象,javascript,typescript,Javascript,Typescript,如何在TypeScript中声明类似JavaScript的嵌套对象 let endpoints = { auth: { login: "http://localhost:8079/auth/login" } }; 以下操作不起作用: private endpoints: Object = { auth: { login: "http://localhost:8079/auth/login" } }; 抛出: 错误TS2339:类

如何在TypeScript中声明类似JavaScript的嵌套对象

let endpoints = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};
以下操作不起作用:

private endpoints: Object = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};
抛出:


错误TS2339:类型“Object”上不存在属性“auth”。

如果要实现类型安全,需要创建自定义类\接口:

interface IMyInterface
{
    auth: IAuth;
}

interface IAuth
{
    login: string;
}

private endpoints: IMyInterface= {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};

您的错误是因为您声明的终结点类型为Object,而Object没有auth属性。

您可以使用接口:

interface EndpointAuth {
    login: string;
}

interface Endpoint {
    auth: EndpointAuth;
}

let endpoints: Endpoint = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};
type EndpointAuth = {
    login: string;
}

type Endpoint = {
    auth: EndpointAuth;
}
()

您还可以使用类型而不是接口:

interface EndpointAuth {
    login: string;
}

interface Endpoint {
    auth: EndpointAuth;
}

let endpoints: Endpoint = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};
type EndpointAuth = {
    login: string;
}

type Endpoint = {
    auth: EndpointAuth;
}
()

或“内联”:

当然,你可以把它们结合起来


编辑 正如您希望答案解释为什么它不能与
对象一起工作一样:

将变量定义为
Object
类型(在大多数情况下)不是您真正想要做的,通常您的意思是
any
,如下所示:

var endpoints2: any = {
    auth: {
        login: "http://localhost:8079/auth/login"
    }
};
不会失败(就像不指定类型不会失败一样)。
将变量定义为
对象
与将其定义为
{}
相同,后者是一个空对象,这通常不是您想要的,并且它只适用于以下情况:

let o1: Object = {};
let o2: Object = Object.create(null);
但是使用
any
对您帮助不大,因为这样您基本上告诉编译器不要担心类型安全性,它将允许您在不让您知道存在错误的情况下对变量执行任何操作:

let o: any = { x: 3, y: 6 };
console.log(o.z.toString());
编译时不会失败,但在运行时会失败:

未捕获的TypeError:无法读取未定义的属性“toString”

这将导致编译失败:

let o: { x: number, y: number } = { x: 3, y: 6 };
console.log(o.z.toString());

您可以声明一个接口

为了你的案子

interface IEndpoints
{
 auth: {
  login: string;
 }
}
private endpoints: IEndpoints = {
  auth: {
    login: "http://localhost:8079/auth/login"
  }
};

我不知道您过去使用的是哪个typescript版本,但目前,它是受支持的

接口端点{
[路径:字符串]:端点|字符串
}
常量终结点:终结点={
认证:{
登录:“http://localhost:8079/auth/login"
}
}

我将此标记为正确答案,因为它是最全面的。然而,阿米尔也解释了错误的性质。如果你愿意的话,你可以把这个加到你的答案上。这很公平。添加了一个详细的解释。让o2:Object=Object.create(null)不正确,因为你说o2的类型是一个以Object为原型的对象,但o2有一个null原型。这应该是更好的答案。当您只需要一个接口/类型时,为什么要定义两个接口/类型呢____完美的____