当前位置:网站首页>No index signature with a parameter
No index signature with a parameter
2022-08-07 16:13:00 【Crispy Bamboo Book Pavilion under the Moon】
TypeScript 索引签名
1.索引签名:No index signature with a parameter
No index signature with a parameter of type 'string' was found on type 'IStatusType'
解释:not in type "IStatusType” Argument type is found on "string"的索引签名
- Error setting
// 返回值状态
interface IStatusType{
400:string,
401:string,
403:string,
404:string,
405:string,
500:string,
502:string,
}
// 定义参数
const _status:IStatusType= {
400:"请求错误",
401:"登录已过期,请重新登录",
403:"禁止访问",
404:"接口不存在",
405:"资源被禁止",
500:"内部服务器错误",
502:"网关错误"
}
// Returns the result example:err.response.data.status === 400
const errStatus:string = err.response.data.status;
/*报错: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'IStatusType'. No index signature with a parameter of type 'string' was found on type 'IStatusType' */
console.log(_status![errStatus]);
- 解决方案:
修改IStatusTypeType setting 就不会报错了
interface IStatusType{
[key: string ]: string;
}
2.什么是索引签名
简单来说:It defines the property names in the object、属性值的类型
tip:TypeScriptThe index signature of must bestring或者number
interface Foo {
[key: string]: number;
x: number;
y: number;
}
或者
索引签名的名称(如:
{ [index: string]: { message: string } }里的index)除了可读性外,并没有任何意义.例如:如果有一个用户名,你可以使用{ username: string}: { message: string },这It is helpful for the next developer to understand your code.
const foo: {
[index: string]: {
message: string };
} = {
};
// 储存的东西必须符合结构
// ok
foo['a'] = {
message: 'some message' };
- Limited string literals
通常与
keyof/typeof一起使用,来获取变量的类型
type Index = 'a' | 'b' | 'c';
type FromIndex = {
[k in Index]?: number };
const good: FromIndex = {
b: 1, c: 2 };
边栏推荐
- First acquaintance with WeChat applet development
- 轻量级运维工具pssh
- A brief description of the function stack
- "Short Video" Gansu Province introduced the "14th Five-Year" cold chain logistics high-quality development plan to create a 1-hour fresh agricultural product logistics circle
- 5步详解如何运用设计思维
- MySQL的UPDATE及SELECT...FOR UPDATE语句关于锁的一些简单验证
- leetcode:636. 函数的独占时间【栈模拟】
- 差点被ECCV错过的Oral论文:视频理解新框架,仅用微调的「成本」,达到预训练的「全能」
- 股市手机开户流程安全吗 中国证券公司前100名
- 907. 区间覆盖
猜你喜欢

ETCD Quick Start - 01 ETCD Overview

第三⼗章 ⼿机端-⼩d课堂在线教育系统效果演示和技术准备

mysql5.7.35安装配置教程【超级详细安装教程】

Chapter 219 New SSM Integration - Getting Through Mysql Database Control Transactions (including interview questions)

简记_Altium Designer 相同模块的布局布线复用

基于Web的商城后台管理系统的设计与实现

Discuz论坛网站搭建教程,从0开始学会搭建网站

Understanding of bubble sort

zabbix monitors Huawei routers

冒泡排序的理解
随机推荐
845. Eight Numbers
MySQL 指令
pip使用豆瓣镜像源
A brief description of the function stack
873. Euler Functions
第13章_事务基础知识
MySQL5.7 installation and configuration tutorial (graphics and text super detailed version)
Brief Notes_How to use the schematic symbols and packages of LiChuang Mall
内置函数和运算符的使用
1712: HDU ACboy needs your help please grouping knapsack problem
Qt opens and creates projects, runs programs
LeetCode每日两题02:买股票的最佳时机 (均1200道)
MySQL的UPDATE及SELECT...FOR UPDATE语句关于锁的一些简单验证
小技巧——postman get&&post请求的使用方式
Qt 打开以及创建项目、运行程序
手机开户股票开户安全吗 股票开户流程具体是什么
LeetCode daily two 02: the best time to buy stocks (1200)
用手机开户买股票安全吗 如何网上基金定投
SAP CRM Fiori 应用 My Opportunity 的分页读取逻辑,在 GM4 - AG3 无法正常工作
ceph cluster