我正在寻找一种拥有嵌套对象的所有键/值对的方法。
(用于 MongoDB 点符号键/值类型的自动完成)
interface IPerson {
name: string;
age: number;
contact: {
address: string;
visitDate: Date;
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我想要实现的目标,使其成为:
type TPerson = {
name: string;
age: number;
contact: { address: string; visitDate: Date; }
"contact.address": string;
"contact.visitDate": Date;
}
Run Code Online (Sandbox Code Playgroud)
在这个答案中,我可以得到密钥Leaves<IPerson>。于是就变成了'name' | 'age' | 'contact.address' | 'contact.visitDate'。
在 @jcalz 的另一个答案中,我可以通过DeepIndex<IPerson, ...>.
是否有可能将它们组合在一起,成为类似的类型TPerson?
当我开始这个问题时,我在想它可以像这样简单[K in keyof T]: T[K];,只需进行一些巧妙的转换。但是我错了。这是我需要的:
所以界面
interface IPerson { …Run Code Online (Sandbox Code Playgroud) javascript mongodb mongodb-query typescript typescript-generics
我有这些一般定义:
type Module<P extends Payloads, C extends Children> = {
payloads: P;
children: C;
};
type Children = Record<string, any>; // some values will be nested Modules
type Payloads = Record<string, any>;
type ExtractPayloads<M extends Module<any, any>> = M extends Module<infer P, any> ? P : never;
type ExtractChildren<M extends Module<any, any>> = M extends Module<any, infer C> ? C : never;
Run Code Online (Sandbox Code Playgroud)
基本上,模块是指定类型的类型children,可以包含嵌套模块。
我有这种类型,可以Action根据模块的payload类型生成 s:
type ModuleRootActions<
MODULE extends Module<any, any>,
PAYLOADS = …Run Code Online (Sandbox Code Playgroud) 是否可以以这样的方式键入字符串数组,使得该数组只能是给定对象中的有效属性路径?类型定义应该适用于所有深度嵌套的对象。
例子:
const object1 = {
someProperty: true
};
const object2 = {
nestedObject: object1,
anotherProperty: 2
};
type PropertyPath<Type extends object> = [keyof Type, ...Array<string>]; // <-- this needs to be improved
// ----------------------------------------------------------------
let propertyPath1: PropertyPath<typeof object1>;
propertyPath1 = ["someProperty"]; // works
propertyPath1 = ["doesntExist"]; // should not work
let propertyPath2: PropertyPath<typeof object2>;
propertyPath2 = ["nestedObject", "someProperty"]; // works
propertyPath2 = ["nestedObject", "doesntExist"]; // should not work
propertyPath2 = ["doesntExist"]; // should not work
Run Code Online (Sandbox Code Playgroud)
是否可以在react-i18next字典中键入检查现有键?因此,如果密钥不存在,TS 会在编译时警告您。
例子。
假设,我们有这本字典:
{
"footer": {
"copyright": "Some copyrights"
},
"header": {
"logo": "Logo",
"link": "Link",
},
}
Run Code Online (Sandbox Code Playgroud)
如果我提供不存在的密钥,TS 应该会爆炸:
const { t } = useTranslation();
<span> { t('footer.copyright') } </span> // this is OK, because footer.copyright exists
<span> { t('footer.logo') } </span> // TS BOOM!! there is no footer.logo in dictionary
Run Code Online (Sandbox Code Playgroud)
这种技术的正确名称是什么?我很确定我不是唯一一个要求这种行为的人。
它是react-i18next开箱即用的吗?是否有 APIreact-i18next以某种方式扩展库以启用它?我想避免创建包装函数。
假设我有以下界面:
interface Foo {
foo: string
bar?: number
nested: {
foo: string,
deeplyNested: {
bar?: number
}
}
union: string | {
foo: string,
bar?: number
}
}
Run Code Online (Sandbox Code Playgroud)
现在我需要一种类型来将此接口转换为扁平版本,键中包含完整路径名,如下所示:
interface FooFlattened {
foo: string
bar?: number
"nested.foo": string
"nested.deeplyNested.bar"?: number
// if union is a string:
union: string
// if union is an object:
"union.foo": string
"union.bar"?: number
}
Run Code Online (Sandbox Code Playgroud)
该类型应该适用于任何级别的嵌套和任何数量的联合成员。
我已经在这里找到了一个相关的问题,但这只是一个非常基本的界面,没有联合或可选参数。
我有一个嵌套对象,我想将其展平,同时还连接嵌套键。我想从这里开始:
const nested = {
a: {
b: {
c: {
d1: "hello",
d2: 42,
},
},
},
};
Run Code Online (Sandbox Code Playgroud)
对此:
const flattened = {
"a.b.c.d1": "hello",
"a.b.c.d2": 42,
};
Run Code Online (Sandbox Code Playgroud)
如果我有一个执行此转换的函数,例如
function flatten<TNested>(nested: TNested): Flattened<TNested> {...}
Run Code Online (Sandbox Code Playgroud)
是否可以定义Flattened执行正确操作并正确推断 的类型的类型flattened?
我知道当前的 TypeScript 可能无法实现这一点,但我想确定一下!
我正在寻找嵌套的对象路径:这可能吗?
interface IHuman {
age: number;
friend: {
name: string;
}
}
keyof IHuman; // "age", "friend.name"
Run Code Online (Sandbox Code Playgroud) 我有一个嵌套的翻译字符串对象,如下所示:
viewName: {
componentName: {
title: 'translated title'
}
}
Run Code Online (Sandbox Code Playgroud)
我使用一个接受点表示法字符串的翻译库来获取字符串,就像这样translate('viewName.componentName.title')。
有什么方法可以强制 translate 的输入参数遵循带有打字稿的对象的形状吗?
我可以通过这样做为第一级做到这一点:
translate(id: keyof typeof languageObject) {
return translate(id)
}
Run Code Online (Sandbox Code Playgroud)
但我希望这种类型是嵌套的,这样我就可以像上面的例子一样确定我的翻译范围。
鉴于 Typescript 中发现的这段(令人惊奇的)代码 :嵌套对象的深层 keyof
type Cons<H, T> = T extends readonly any[] ?
((h: H, ...t: T) => void) extends ((...r: infer R) => void) ? R : never
: never;
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]
type Paths<T, D extends number = 10> = [D] extends [never] ? never : T extends object ?
{ [K in keyof …Run Code Online (Sandbox Code Playgroud) 我在Typescript: deep keyof of a Nested object中看到了类似这样的类型定义:
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, ...0[]]
Run Code Online (Sandbox Code Playgroud)
我不明白最后一部分是什么...0[]意思。
有这方面的文件吗?谢谢
通过添加模板文字类型,现在可以以类型安全的方式表达属性路径(点符号)。一些用户已经使用模板文字类型实现了一些东西或者提到了它。
我想更进一步,并表达类型中空值/未定义/可选的可能性,例如foo.bar?.foobar,foo.boo.far?.farboo编译器应该可以接受,foo.bar.foobar而不适用于以下类型:
type Test = {
foo: {
bar?: {
foobar: never;
barfoo: string;
};
foo: symbol;
boo: {
far:
| {
farboo: number;
}
| undefined;
};
};
};
Run Code Online (Sandbox Code Playgroud)
到目前为止,可选参数被拾取(我不知道为什么,但它在我的 IDE 中使用相同的打字稿版本,请参见下面的屏幕截图)但不是明确标记的“远”属性作为未定义。这个游乐场展示了我的进步。不知何故,“未定义检查”没有按预期工作。
我想找到一种方法来拥有value types嵌套对象路径的所有键。
我在一定程度上取得了成功,但未能为数组对象内的深层嵌套属性设置值类型。
interface BoatDetails {
boats: {
boat1: string;
boat2: number;
};
ships: Array<Ship>
}
interface Ship {
shipName: string
}
const boatDetails: BoatDetails = {
boats: {
boat1: "lady blue",
boat2: 1,
},
ships: [
{shipName: "lacrose"}
]
};
Run Code Online (Sandbox Code Playgroud)
对于上面的代码,我能够成功设置嵌套对象路径的值类型,例如boats.boat1who value type is string、boats.boat2who value type is number、shipswho value type is Array<Ship>。
但无法设置value type嵌套路径ships.0.shipName。
我从以下链接中参考了设置深层嵌套对象路径类型: Typescript:嵌套对象的深层 keyof
下面是我在打字稿游乐场中设置深层嵌套对象路径的值类型的尝试:
typescript ×12
i18next ×1
javascript ×1
jsonpointer ×1
mongodb ×1
recursion ×1
reflection ×1
types ×1