假设我有界面
interface X {
a: string;
b: number;
c: boolean;
}
Run Code Online (Sandbox Code Playgroud)
和一个功能
function values(x: X) {
return Object.keys(x).map(s => x[s])
}
Run Code Online (Sandbox Code Playgroud)
当我启用typescript的strict标志时,我得到错误"元素隐式具有'任何'类型,因为类型'X'没有索引签名".因此,为了使其明确,我可以只为X的定义添加索引签名
[key: string]: any;
Run Code Online (Sandbox Code Playgroud)
十分简单.
但是,如果IX现在是映射类型,则:
type X<T> = {
[P in keyof T]: string;
}
Run Code Online (Sandbox Code Playgroud)
我有这个功能
function values<T>(x: X<T>) {
return Object.keys(x).map(s => x[s])
}
Run Code Online (Sandbox Code Playgroud)
我应该在哪里添加索引签名?有没有办法让这个明确,而不诉诸做一些粗略的事情Object.keys(x).map(s => (x as any)[s])
我写TypeScript已有一段时间了,对索引签名的含义感到困惑。
例如,此代码是合法的:
function fn(obj: { [x: string]: number }) {
let n: number = obj.something;
}
Run Code Online (Sandbox Code Playgroud)
但是,执行相同操作的代码不是:
function fn(obj: { [x: string]: number }) {
let p: { something: number } = obj;
}
Run Code Online (Sandbox Code Playgroud)
这是错误吗?这是什么意思?
从旧的 api 我得到一个 JSON 响应,如下所示:
const someObject = {
"general": {
"2000": 50,
"4000": 100,
"8000": 200,
},
"foo": [
0,
1,
2,
],
"bar": [
5,
7,
],
"baz": [
8,
9,
],
};
Run Code Online (Sandbox Code Playgroud)
请记住,除“general”之外的所有索引都是动态的,可能不在响应中,我无法为每个属性键入但必须使用索引签名。
我想通过 typescript@2.9.2 来实现:
interface ISomeObject {
general: {
[index: string]: number;
};
[index: string]?: number[];
}
Run Code Online (Sandbox Code Playgroud)
因为general总是会在响应,但其他指标可能会或可能不会在那里。
我面临的问题:
[index: string]?: number[]可选的,因为它会抱怨这里使用数字作为值。[index: string]: number[]将覆盖的定义,general: number因此 tsc 会抱怨:
Property 'general' of type '{ [index: string]: number; }' is …Run Code Online (Sandbox Code Playgroud)你如何输入一个可以同时拥有几个声明的可选属性的对象,例如:
{
hello?: string,
moo?: boolean
}
Run Code Online (Sandbox Code Playgroud)
以及自定义属性(必须是函数),例如:
[custom: string]: (v?: any) => boolean
Run Code Online (Sandbox Code Playgroud)
这是我希望看到的,例如:
const myBasic: Example = {moo: false}
// -> ? Valid! Using known keys
const myValid: Example = {hello: 'world', customYo: () => true}
// -> ? Valid! "customYo" is a function returning a bool. Good job!
const myInvalid: Example = {hello: 'world', customYo: 'yo!'}
// -> ?? Invalid! "customYo" must be a function returning a boolean
Run Code Online (Sandbox Code Playgroud)
尝试将索引签名添加到具有已知键的接口(即hello?: string, moo?: boolean)需要所有键都是索引签名类型的子集(在这种情况下,返回 …
我正在尝试使用reduce与Typescript来达到传入消息的总数.我对如何添加索引签名很困惑.我一直收到错误:"元素隐式有'任何'类型,因为类型'{}'没有索引签名." on变量newArray和count [k].我已经阅读了几个类似的问题,但还没有弄清楚如何将它们应用到我的特定场景中.我是JavaScript和TypeScript的新手.
这是我的数组:
var data = [
{ time: '9:54' },
{ time: '9:54' },
{ time: '9:54' },
{ time: '9:55' },
{ time: '9:55' },
{ time: '9:55' },
{ time: '9:55' },
{ time: '9:56' },
{ time: '9:56' },
{ time: '9:56' },
{ time: '9:56' },
{ time: '9:57' },
{ time: '9:57' },
{ time: '9:57' },
{ time: '9:57' },
{ time: '9:57' }
];
Run Code Online (Sandbox Code Playgroud)
这就是我需要在rechart图中使用我的数组的方法:
var dataWithCounts = [
{ time: …Run Code Online (Sandbox Code Playgroud) 假设接口有一些已知的属性及其类型,并且可以有其他具有未知键和一些其他类型的属性,例如:
interface Foo {
length: number;
[key: string]: string;
}
const foo : Foo = {
length: 1,
txt: "TXT",
};
Run Code Online (Sandbox Code Playgroud)
TS错误:
'number' 类型的属性 'length' 不能分配给字符串索引类型 'string'。
应该如何键入这样的接口?
我遇到以下问题,我需要使用 forEach 循环迭代 Angular 中表单的控件属性。我正在编写以下代码:
const arr = this.bankForm.controls;
arr.forEach((element: {[key: string]: AbstractControl}) => {
});
Run Code Online (Sandbox Code Playgroud)
我有下一个错误:
typescript ×7
angular ×1
angular5 ×1
arrays ×1
interface ×1
javascript ×1
json ×1
mapped-types ×1