首页   注册   登录

yuanfnadi

V2EX 第 95215 号会员,加入于 2015-02-01 22:45:59 +08:00
今日活跃度排名 17763
yuanfnadi 最近回复了
```
interface PathArray<T, L> extends Array<string | number> {
["0"]?: keyof T;
["1"]?: L extends {
["0"]: infer K0;
}
? K0 extends keyof T
? keyof T[K0]
: never
: never;
["2"]?: L extends {
["0"]: infer K0;
["1"]: infer K1;
}
? K0 extends keyof T
? K1 extends keyof T[K0]
? keyof T[K0][K1]
: never
: never
: never;
["3"]?: L extends {
["0"]: infer K0;
["1"]: infer K1;
["2"]: infer K2;
}
? K0 extends keyof T
? K1 extends keyof T[K0]
? K2 extends keyof T[K0][K1]
? keyof T[K0][K1][K2]
: never
: never
: never
: never;
["4"]?: L extends {
["0"]: infer K0;
["1"]: infer K1;
["2"]: infer K2;
["3"]: infer K3;
}
? K0 extends keyof T
? K1 extends keyof T[K0]
? K2 extends keyof T[K0][K1]
? K3 extends keyof T[K0][K1][K2]
? keyof T[K0][K1][K2][K3]
: never
: never
: never
: never
: never;
["5"]?: L extends {
["0"]: infer K0;
["1"]: infer K1;
["2"]: infer K2;
["3"]: infer K3;
["4"]: infer K4;
}
? K0 extends keyof T
? K1 extends keyof T[K0]
? K2 extends keyof T[K0][K1]
? K3 extends keyof T[K0][K1][K2]
? K4 extends keyof T[K0][K1][K2][K3]
? keyof T[K0][K1][K2][K3][K4]
: never
: never
: never
: never
: never
: never;
["6"]?: L extends {
["0"]: infer K0;
["1"]: infer K1;
["2"]: infer K2;
["3"]: infer K3;
["4"]: infer K4;
["5"]: infer K5;
}
? K0 extends keyof T
? K1 extends keyof T[K0]
? K2 extends keyof T[K0][K1]
? K3 extends keyof T[K0][K1][K2]
? K4 extends keyof T[K0][K1][K2][K3]
? K5 extends keyof T[K0][K1][K2][K3][K4]
? keyof T[K0][K1][K2][K3][K4][K5]
: never
: never
: never
: never
: never
: never
: never;
}

type ArrayHasIndex<MinLenght extends number> = { [K in MinLenght]: any };

export type PathArrayValue<
T,
L extends PathArray<T, L>
> = L extends ArrayHasIndex<0 | 1 | 2 | 3 | 4 | 5 | 6 | 7>
? any
: L extends ArrayHasIndex<0 | 1 | 2 | 3 | 4 | 5 | 6>
? T[L[0]][L[1]][L[2]][L[3]][L[4]][L[5]][L[6]]
: L extends ArrayHasIndex<0 | 1 | 2 | 3 | 4 | 5>
? T[L[0]][L[1]][L[2]][L[3]][L[4]][L[5]]
: L extends ArrayHasIndex<0 | 1 | 2 | 3 | 4>
? T[L[0]][L[1]][L[2]][L[3]][L[4]]
: L extends ArrayHasIndex<0 | 1 | 2 | 3>
? T[L[0]][L[1]][L[2]][L[3]]
: L extends ArrayHasIndex<0 | 1 | 2>
? T[L[0]][L[1]][L[2]]
: L extends ArrayHasIndex<0 | 1>
? T[L[0]][L[1]]
: L extends ArrayHasIndex<0>
? T[L[0]]
: never;

export type Path<T, L> = PathArray<T, L> | keyof T;

export type PathValue<T, L extends Path<T, L>> = L extends PathArray<T, L>
? PathArrayValue<T, L>
: L extends keyof T
? T[L]
: any;

declare function path<T, L extends Path<T, L>>(
object: T,
params: L
): PathValue<T, L>;

const obj = {
v: {
w: { x: { y: { z: { a: { b: { c: 2 } } } } } },
ouch: true,
foo: [{ bar: 2 }, { bar: 3 }]
}
};

const output: number = path(obj, ["v", "w", "x"]); // 💥
const output2: object = path(obj, ["v", "w", "x"]); // ✔️
const output4: { c: string } = path(obj, ["v", "w", "x", "y", "z", "a", "b"]); // 💥
const output3: { c: number } = path(obj, ["v", "w", "x", "y", "z", "a", "b"]); // ✔️
const output5: "wrong" = path(obj, ["v", "w", "x", "y", "z", "a", "b", "c"]); // ✔️ since after 7 levels there is no typechecking

const x = path(obj, ["v", "ouch", "x"]); // 💥
const y = path(obj, ["v", "ouch", "y"]); // 💥
const z = path(obj, ["v", "ouch", "somethingCompletelyDifferent"]); // 💥

path(obj, "!"); // 💥
path(obj, ["!"]); // 💥
path(obj, ["v", "!"]); // 💥
path(obj, ["v", "w", "!"]); // 💥
path(obj, ["v", "w", "x", "!"]); // 💥
path(obj, ["v", "w", "x", "y", "!"]); // 💥
path(obj, ["v", "w", "x", "y", "z", "!"]); // 💥
path(obj, ["v", "w", "x", "y", "z", "a", "!"]); // 💥
path(obj, ["v", "w", "x", "y", "z", "a", "b", "!"]); // ✔️ since after 7 levels there is no typechecking
path(obj, "v"); // ✔️
path(obj, ["v"]); // ✔️
path(obj, ["v", "w"]); // ✔️
path(obj, ["v", "w", "x"]); // ✔️
path(obj, ["v", "w", "x", "y"]); // ✔️
path(obj, ["v", "w", "x", "y", "z"]); // ✔️
path(obj, ["v", "w", "x", "y", "z", "a"]); // ✔️
path(obj, ["v", "w", "x", "y", "z", "a", "b"]); // ✔️
path(obj, ["v", "w", "x", "y", "z", "a", "b", "c"]); // ✔️
```
一个月一天就够了。

hexo + 小程序(webview) + 安卓 (webview)
49 天前
回复了 Poto 创建的主题 程序员 [调查] 大家如何管理电脑的文件
+ 音乐用 itunes 管理
+ 代码用 projj
+ 文档放在语雀
+ 软件用 apple store + setapp
+ 电子书放 calibre

总结,文档分类,然后用不同的软件管理。
@eicky 可以试试看,然后指出哪些功能不如大象。
@Kahnn 试试看 clipper.website.这样你只需要用 notion 就好了。
50 天前
回复了 everettjf 创建的主题 分享创造 远程输入法发布,用浏览器给手机打字
Mac 复制到剪切板,手机上直接粘贴就好了。
@how2ex 我上面发的那个插件,开源免费的,可以保存网页到 语雀,有道云,onenote GitHub bear notion。而且可以安装插件的插件来扩展

如果需要适配特定的网站,可以联系上面的邮箱。
@how2ex 怎么联系
https://clipper.website
关于   ·   FAQ   ·   API   ·   我们的愿景   ·   广告投放   ·   感谢   ·   实用小工具   ·   854 人在线   最高记录 5043   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.3 · 9ms · UTC 21:35 · PVG 05:35 · LAX 13:35 · JFK 16:35
♥ Do have faith in what you're doing.