首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript类型安全服务注册表

TypeScript类型安全服务注册表
EN

Stack Overflow用户
提问于 2021-03-19 13:40:20
回答 1查看 96关注 0票数 2

如何在Typescript中创建类型安全服务注册表。基本上,这是一个根据参数返回具体类型的函数。

代码语言:javascript
复制
get("a") would return an object of type ServiceA.
get("b") would return an object of type ServiceB.

我按照这里的答案:https://stackoverflow.com/a/47098963/12116498,但仍然收到输入错误。

我已经将我的问题简化为以下内容,并添加了我得到的类型错误:

代码语言:javascript
复制
console.log("Test get");
console.log("Get foo from AlertsService:", get('AlertsService').foo);
console.log("Get foo from AlertsService:", get('ModalService').foo);
console.log("Get foo from AlertsService:", get('ModalService').bar);
 
function get<T extends keyof ServiceMapping>(serviceType: T): ServiceMapping[T] {
  const t: keyof ServiceMapping = serviceType;
 
  switch (t) {
    case 'AlertsService':
 
// TS2322: Type '{ type: "AlertsService"; foo: string; }' is not assignable to type 'ServiceMapping[T]'.
// Type '{ type: "AlertsService"; foo: string; }' is not assignable to type 'never'.
// The intersection 'AlertsService & ModalService' was reduced to 'never' because property 'type' has conflicting types in some constituents.
 
      return {
        type: 'AlertsService',
        foo: 'testing'
      };
    case 'ModalService':
     
// TS2322: Type '{ type: "ModalService"; foo: string; bar: string; }' is not assignable to type 'ServiceMapping[T]'.
// Type '{ type: "ModalService"; foo: string; bar: string; }' is not assignable to type 'never'.
// The intersection 'AlertsService & ModalService' was reduced to 'never' because property 'type' has conflicting types in some constituents.
     
      return {
        type: 'ModalService',
        foo: 'testing',
        bar: 'bar test'
      };
 
    default:
      throw new Error("nope");
  }
}
 
export type ServiceMapping = {
  AlertsService: AlertsService;
  ModalService: ModalService;
}
 
export type AlertsService = {
  type: 'AlertsService',
  foo: string
}
 
export type ModalService = {
  type: 'ModalService',
  foo: string,
  bar: string
}

重要的是要将返回类型限定在较窄的范围内,而不是其他类型的联合。

EN

回答 1

Stack Overflow用户

发布于 2021-03-22 19:09:16

@kruschid请告诉我为什么超载不好

代码语言:javascript
复制
  type AlertsService = {
    type: 'AlertsService',
    foo: string,
  }

  type ModalService = {
    type: 'ModalService',
    foo: string,
    bar: string
  }

  function get<T extends 'AlertsService'>(arg: 'AlertsService'): AlertsService
  function get<T extends 'ModalService'>(arg: T): ModalService
  function get<T extends 'AlertsService' | 'ModalService'>(arg: T) {

    switch (arg) {
      case 'AlertsService':
        return {
          type: 'AlertsService',
          foo: 'testing'
        };
      case 'ModalService':
        return {
          type: 'ModalService',
          foo: 'testing',
          bar: 'bar test'
        };

      default:
        throw new Error("nope");
    }
  }


  const result = get('AlertsService') // AlertsService
  const result2 = get('ModalService') // ModalService
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66703040

复制
相关文章

相似问题

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