首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于输入参数的TypeScript函数返回类型

问基于输入参数的TypeScript函数返回类型
EN

Stack Overflow用户
提问于 2019-01-13 02:07:08
回答 6查看 53.8K关注 0票数 97

我有几个不同的接口和对象,每个接口和对象都有一个type属性。假设这些对象存储在NoSQL db中。如何根据输入参数getItem创建具有确定性返回类型的泛型type函数?

代码语言:javascript
复制
interface Circle {
    type: "circle";
    radius: number;
}

interface Square {
    type: "square";
    length: number;
}

const shapes: (Circle | Square)[] = [
    { type: "circle", radius: 1 },
    { type: "circle", radius: 2 },
    { type: "square", length: 10 }];

function getItems(type: "circle" | "square") {
    return shapes.filter(s => s.type == type);
    // Think of this as items coming from a database
    // I'd like the return type of this function to be
    // deterministic based on the `type` value provided as a parameter. 
}

const circles = getItems("circle");
for (const circle of circles) {
    console.log(circle.radius);
                       ^^^^^^
}

属性'radius‘不存在于“圆环”平方上。

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2019-01-13 04:06:16

Conditional Types到救援:

代码语言:javascript
复制
interface Circle {
    type: "circle";
    radius: number;
}

interface Square {
    type: "square";
    length: number;
}

type TypeName = "circle" | "square"; 

type ObjectType<T> = 
    T extends "circle" ? Circle :
    T extends "square" ? Square :
    never;

const shapes: (Circle | Square)[] = [
    { type: "circle", radius: 1 },
    { type: "circle", radius: 2 },
    { type: "square", length: 10 }];

function getItems<T extends TypeName>(type: T) : ObjectType<T>[]  {
    return shapes.filter(s => s.type == type) as ObjectType<T>[];
}

const circles = getItems("circle");
for (const circle of circles) {
    console.log(circle.radius);
}

谢谢西尔维奥为我指明了正确的方向。

票数 157
EN

Stack Overflow用户

发布于 2019-01-13 02:13:28

你在找过载签名

代码语言:javascript
复制
function getItems(type: "circle"): Circle[]
function getItems(type: "square"): Square[]
function getItems(type: "circle" | "square") {
    return shapes.filter(s => s.type == type);
}

在实际定义之前放置多个类型签名可以让您列出函数签名可能落入的不同“情况”。

评论后的编辑()

所以事实证明,你想要的是可能的,但我们可能要跳过几圈才能到达那里。

首先,我们需要一个方法来翻译每个名字。我们希望"circle"映射到Circle,"square"到Square等。为此,我们可以使用条件类型。

代码语言:javascript
复制
type ObjectType<T> =
  T extends "circle" ? Circle :
  T extends "square" ? Square :
  never;

(我使用never作为后盾,希望它在某种程度上导致无效类型时会很快创建类型错误)

现在,我不知道像您所要求的那样对函数调用的类型进行参数化的方法,但是类型记录确实支持通过映射类型对对象的键进行参数化。因此,如果您愿意将getItems("circle")语法转换为getItems["circle"],我们至少可以描述这种类型。

代码语言:javascript
复制
interface Keys {
  circle: "circle";
  square: "square";
}

type GetItemsType = {
  [K in keyof Keys]: ObjectType<K>[];
}

问题是,我们现在必须构建一个这种类型的对象。如果以ES2015 (编译时的--target es2015或更新版本)为目标,则可以使用Javascript Proxy类型。现在,不幸的是,我不知道有什么好的方法来说服类型抄本,我们正在做的是好的,所以通过any的快速投射将平息它的担忧。

代码语言:javascript
复制
let getItems: GetItemsType = <any>new Proxy({}, {
  get: function(target, type) {
    return shapes.filter(s => s.type == type);
  }
});

因此,您在实际的getItems“函数”上丢失了类型检查,但是在调用站点上获得了更强的类型检查。然后,打电话,

代码语言:javascript
复制
const circles = getItems["circle"];
for (const circle of circles) {
    console.log(circle.radius);
}

这值得吗?那随你的便。这是很多额外的语法,您的用户必须使用[]表示法,但是它得到了您想要的结果。

票数 53
EN

Stack Overflow用户

发布于 2022-04-07 20:08:27

我遇到了一个类似的问题。如果您不想同时拥有TypeName和ObjectType类型,也可以使用单个接口来完成:

代码语言:javascript
复制
interface TypeMap {
  "circle": Circle;
  "square": Square;
}

function getItems<T extends keyof TypeMap>(type: T) : TypeMap[T][]  {
  return shapes.filter(s => s.type == type) as TypeMap[T][];
}
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54165536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档