写接口类型时,希望一个类型的键值是联合类型中固定的几个

const enum MSGTYPE{TEXT = 'text',IMAGE = 'image',
}
// or
// type MSGTYPE = 'text' | 'image';export interface QywxSendMessage = {msg_id: number;msg_name: string;[key: MSGTYPE]: number;
}

显然会报错An index signature parameter type cannot be a union type. Consider using a mapped object type instead. ts(1337)

问题及解决方式呢:
https://github.com/microsoft/TypeScript/issues/33440
https://github.com/Microsoft/TypeScript/issues/24220
https://github.com/Microsoft/TypeScript/issues/2491

export interface QywxSendMessage = {msg_id: number;msg_name: string;[key in MSGTYPE]: number;
}

写接口类型时,希望一个类型的键值是联合类型中固定的几个中的一个

type MSGTYPE = 'text' | 'image';export interface QywxSendMessage = {msg_id: number;msg_name: string;[key: MSGTYPE]: number;// [key: 'text' | 'image']: number;
}

显然也会报错An index signature parameter type cannot be a union type. Consider using a mapped object type instead. ts(1337)

那该如何表示呢?如果用?那显然是有问题的

type MSGTYPE<T> = {'text': T } | {'image':T };export interface QywxSendMessageParams = {msg_id: number;msg_name: string;
}export type QywxSendMessage = QywxSendMessageParams & MSGTYPE<number>;

Typescript ques:An index signature parameter type cannot be a union type. Consider using a mapped...相关推荐

  1. C++报错解决:error: ‘int’ is not a class, struct, or union type typedef typename _Sequence::value_

    我在使用C++的时候,遇到了如下报错: -> % g++ test.cpp In file included from /usr/include/c++/7/queue:64:0,from te ...

  2. System.InvalidOperationException:Each parameter in the deserialization constructor on type ‘‘ must

    报错内容: System.InvalidOperationException:"Each parameter in the deserialization constructor on ty ...

  3. TypeScript里的Index signature

    参考链接:https://basarat.gitbook.io/typescript/type-system/index-signatures 看一个例子: class Foo {constructo ...

  4. 报错:Parameter ‘XXX‘ implicitly has an ‘any‘ type.解决方法

    报错:Parameter 'XXX' implicitly has an 'any' type.解决方法 tsconfig.json添加"noImplicitAny": false ...

  5. Parameter ‘xxx’ implicitly has an ‘any’ type的解决

    Parameter 'xxx' implicitly has an 'any' type的解决 这句话翻译过来就是参数暗指是any类型 在TS中等同于以下代码: const f = (param: a ...

  6. 关于 TypeScript 联合类型 union type 赋值的一个错误消息

    今天我做开发时遇到如下的 TypeScript 错误: Type 'Observable<boolean | undefined>' is not assignable to type ' ...

  7. WordPress 主题教程 #3:开始 Index.php

    开始 Index.php 是从零开始创建 WordPress 主题系列教程的第三篇.在介绍了 WordPress 主题的一些规则和术语,以及对 WordPress 模板和模板文件了解之后,现在是开始动 ...

  8. 【es】Elasticsearch:inverted index,doc_values及source

    文章目录 1.概述 2.Inverted index 3.Source 4.Doc_values 1.概述 转载:https://www.cnblogs.com/sanduzxcvbnm/p/1208 ...

  9. Sourcetree git status失败 错误代码128:error :bad signature 0x0000000

    git status失败 错误代码128:error :bad signature 0x0000000 今天使用Sourcetree 报了这样的错 已解决: 第一步先删除 index rm -f .g ...

最新文章

  1. 如何给5岁孩子解释DCT?
  2. 移动端Click300毫秒点击延迟 解决办法
  3. android平台使用java动态生成公私钥,并导出证书文件
  4. 糖豆人维修服务器多长时间,服务器不稳定的《糖豆人》凭啥还这么火?只因做到了这三点...
  5. mysql工厂模式_工厂模式IDAL详解
  6. ae合成复制脚本_【脚本】AE脚本精选系列 | 合成复制脚本 True Comp Duplicator v3.9.7...
  7. web版微信自动发消息(实现微信个人号机器人)
  8. Firefox downloadhelper 视频下载助手
  9. html如何调用function,请问HTML function函数怎么定义和调用?
  10. Sqlite数据库对时间进行比较SQL语句
  11. 怎样练出完美肌肉:[2]完美背肌
  12. 国防科大计算机学院贾焰,博士群体竞风流——记国防科大教员队伍的变化
  13. Javascript捕捉(capturing)与冒泡(bubbling)的区别
  14. Android Studio汉化(中文支持)
  15. GCC汇编源码中的.rept关键字
  16. BOJ 1433 Tom 的潜望镜 优先队列+BFS
  17. ThinkPad电脑黑屏只显示鼠标
  18. python文本发音_python3 - 文本读音器
  19. 在虚拟机中使用Ubuntu和windows系统
  20. 删除下拉框只找23火星软件_下拉框优化首选23火星软件

热门文章

  1. 日期时间表示格式大全: ISO 8601、RFC、时间戳、Excel...
  2. 求两个不超过 200 位的非负整数的和(逐句解释)
  3. 手推反向传播+numpy实现
  4. div 设置a4大小_CSS设置A4纸张尺寸
  5. 病毒传播建模SEIR与网络传播
  6. openjudge 二维数组右上坐下遍历(二维数组)
  7. 新疆大学 学计算机怎么样,一战考东部沿海211计算机失利,调剂到新疆大学,去读还是二战?...
  8. 回避与趋近性负性人格特质对应激心血管反应模式
  9. github更新网站内容,然后在本地commit,pull,push后,刷新网站没有出现更新的内容
  10. 产品经理常见面试题目——HR问题