Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/444.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
如果根属性具有变量名,如何在Typescript中键入一行javascript对象_Javascript_Typescript_Typescript Typings_Dynamic Typing - Fatal编程技术网

如果根属性具有变量名,如何在Typescript中键入一行javascript对象

如果根属性具有变量名,如何在Typescript中键入一行javascript对象,javascript,typescript,typescript-typings,dynamic-typing,Javascript,Typescript,Typescript Typings,Dynamic Typing,我有以下javascript对象: var termsAndConditions = { pt: ["url1", "url2"], en: ["url3", "url4"] } 我想在一行中用Typescript键入它。大概是这样的: const termsAndConditions: {[countryKey: Array<string>]} = { pt: ["url1", "url2"], en: ["url3", "url4"] } co

我有以下javascript对象:

var termsAndConditions = {
    pt: ["url1", "url2"],
    en: ["url3", "url4"]
}
我想在一行中用Typescript键入它。大概是这样的:

const termsAndConditions: {[countryKey: Array<string>]} = {
    pt: ["url1", "url2"],
    en: ["url3", "url4"]
}
const ptUrls: Array<string> = termsAndConditions.pt;
const enUrls: Array<string> = termsAndConditions.en;
const termsAndConditions:{[countryKey:Array]}={
pt:[“url1”、“url2”],
中文:[“url3”、“url4”]
}
然后像这样使用它:

const termsAndConditions: {[countryKey: Array<string>]} = {
    pt: ["url1", "url2"],
    en: ["url3", "url4"]
}
const ptUrls: Array<string> = termsAndConditions.pt;
const enUrls: Array<string> = termsAndConditions.en;
const ptUrls:Array=termsAndConditions.pt;
常量:数组=termsAndConditions.en;
我如何才能做到这一点?

您可以这样做:

const termsAndConditions: { [countryKey: string]: string[] } = {
    pt: ["url1", "url2"],
    en: ["url3", "url4"]
}
稍后您不需要添加额外的键入,因为它已经被指定了

例如

您可以这样做:

const termsAndConditions: { [countryKey: string]: string[] } = {
    pt: ["url1", "url2"],
    en: ["url3", "url4"]
}
稍后您不需要添加额外的键入,因为它已经被指定了

例如

类型TermAndConditions=记录
如果类型具有相同的值形状,您可以使用,使用union type作为记录的键将适合您的情况
如果类型具有相同的值形状,您可以使用,使用union type作为记录的键将适合您的情况

从代码中,我看不出您为什么要这样做,一切都可以推断出来。你能详细说明一下吗?您还可以使用类似于
typet1=typeof termsAndConditions[“en”]
的内容。从代码中,我看不出您为什么要这样做,所有内容都可以推断出来。你能详细说明一下吗?您也可以使用类似于
type T1=typeof termsAndConditions[“en”]
的东西。或者如果您愿意,可以使用
Array
代替
string[]
,或者如果您愿意,可以使用
Array
代替
string[]
prefer@arun-vinoth thx,我添加了一些参考链接和explanation@arun-维诺thx,我添加了一些参考链接和解释