typescript - 是否可以修改 TypeScript 中文字的推断类型?

考虑以下尝试有条件地将属性添加到具有推断类型的对象的代码:

const foo = {
    a: 1,
    b: 2,
};

if (bar) {
    foo.c = 3; // Error: Property 'c' does not exist on type '{ a: number; b: number; }'.(2339)
}

可以通过将 foo 的类型显式声明为 { a: number; 来消除错误。 b:数量; c?: 数字; } 或使用价差有条件地添加 c:

const foo = {
    a: 1,
    b: 2,
    ...(bar ? { c: 3 } : {}),
};

但是,假设我们想保留原始代码结构,但我们也想避免必须显式声明可以推断的属性。是否有任何解决方案可以同时满足这两个要求?例如,是否可以以某种方式调整推断类型,例如:

const foo = {
    a: 1,
    b: 2,
} as { ...; c?: number; }; // Example, does not work

最佳答案

这不是很漂亮,但它有效:ab 的属性类型是推断的,不必重复声明。

function withMissingProps<T>() {
  return function<S>(obj: S): S & Partial<T> {
    return obj;
  }
}

const foo = withMissingProps<{ c: number }>()({
  a: 1,
  b: 2
});

if(Math.random() > 0.5) {
  foo.c = 1;
}

有两个类型参数,TS,分别用于声明的和推断的属性。不幸的是,如果一个函数有两个类型参数,那么您必须同时提供或推断两者;解决方案是柯里化(Currying)函数,尽管这意味着要多一对括号。

Playground Link

https://stackoverflow.com/questions/65284452/

相关文章:

c# - 如何使用 MSIX 允许可编辑的 .Net 生成的配置文件?

flutter - 有什么方法可以将查询参数插入 flutter web 中的命名路由?

bootstrap-modal - Bootstrap v5,如何在模式中传递变量(没有 jquer

gpu - Spacy + GPU 给出错误 : GPU is not accessible. 库是

docker - containerd 中的 docker volumes 是什么?

amazon-web-services - 弹性 beantalk 未记录的 venv 路径

css - react/next.js 中的 * 选择器

python - 如何在 Python 的 Tkinter 中使用 .create_image

python - 将前导零添加到日期和时间字符串中的小时数

docker - Docker 中 dotnet/aspnet :3. 1 的 list 条目中没有