英文:
How can I solve errors in TypeScript?
问题
我是TypeScript新手。以下是我的代码,其中在注释中提到了错误。出了什么问题?
type BooleanAttributeName = string;
type PairedAttribute = {
[_: string]: string | number;
};
type CompositeAttribute = [
PairedAttribute,
BooleanAttributeName | BooleanAttributeName[]
];
type Attributes = CompositeAttribute | PairedAttribute;
function SetAttributes(attributes: Attributes) {
if (attributes.length === undefined) {
for (const key in attributes) {
console.log(key, attributes[key]); /错误:因为类型为 'Attributes' 的表达式不能用于索引类型为 'string' 的表达式,所以元素隐含地具有 'any' 类型。在类型 'Attributes' 上找不到具有参数类型 'string' 的索引签名。/
}
}
attributes.map((item) => { /此表达式不可调用。'string | number | ((callbackfn: (value: string | PairedAttribute | string[], index: number, array: (string | PairedAttribute | string[])[]) => U, thisArg?: any) => U[])' 的所有成员都不可调用。类型 'string' 没有调用签名。/ /参数 'item' 隐式具有 'any' 类型。/
if (item.length === undefined) {
for (const key in item) {
console.log(key, item[key]);
}
} else if (typeof item === "object") {
item.map((BooleanAttributeName) => { /参数 'BooleanAttributeName' 隐式具有 'any' 类型。/
console.log(BooleanAttributeName);
});
} else {
console.log(item);
}
});
}
let sample: Attributes = [
{
key1: "value1",
key2: "value2",
},
["BooleanAttribute1", "BooleanAttribute2"],
];
SetAttributes(sample);
我尝试使用了[官方TypeScript文档](https://www.typescriptlang.org/docs/handbook/2/narrowing.html)中的一些缩小类型的概念,但仍然遇到了这些问题。
我想知道如何解决这个问题的解决方案。
英文:
I'm new to TypeScript. Below is my code with errors mentioned in comments. What is going wrong?
type BooleanAttributeName = string;
type PairedAttribute = {
[_: string]: string | number;
};
type CompositeAttribute = [
PairedAttribute,
BooleanAttributeName | BooleanAttributeName[]
];
type Attributes = CompositeAttribute | PairedAttribute;
function SetAttributes(attributes: Attributes) {
if (attributes.length === undefined) {
for (const key in attributes) {
console.log(key, attributes[key]); /*Error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Attributes'. No index signature with a parameter of type 'string' was found on type 'Attributes'.*/
}
}
attributes.map((item) => { /*This expression is not callable. Not all constituents of type 'string | number | (<U>(callbackfn: (value: string | PairedAttribute | string[], index: number, array: (string | PairedAttribute | string[])[]) => U, thisArg?: any) => U[])' are callable. Type 'string' has no call signatures.*/ /*Parameter 'item' implicitly has an 'any' type.*/
if (item.length === undefined) {
for (const key in item) {
console.log(key, item[key]);
}
} else if (typeof item === "object") {
item.map((BooleanAttributeName) => { /*Parameter 'BooleanAttributeName' implicitly has an 'any' type.*/
console.log(BooleanAttributeName);
});
} else {
console.log(item);
}
});
}
let sample: Attributes = [
{
key1: "value1",
key2: "value2",
},
["BooleanAttribute1", "BooleanAttribute2"],
];
SetAttributes(sample);
I tried to use some concepts of narrowing from the official TypeScript docs but still I am facing these problems.
I want to know what can be solution for the problem.
答案1
得分: 1
尝试添加一些类型转换和检查:
type BooleanAttributeName = string;
type PairedAttribute = {
[_: string]: string | number;
};
type CompositeAttribute = [
PairedAttribute,
BooleanAttributeName | BooleanAttributeName[]
];
type Attributes = CompositeAttribute | PairedAttribute;
function SetAttributes(attributes: Attributes) {
if (typeof attributes === 'object') {
const attr = attributes as PairedAttribute;
for (const key in attr) {
console.log(key, attr[key]);
}
}
if (attributes instanceof Array) {
const attr = attributes as CompositeAttribute;
attr.map((item) => {
if (typeof item === "object") {
const it = item as PairedAttribute;
for (const key in it) {
console.log(key, it[key]);
}
} else {
console.log(item);
}
});
}
}
let sample: Attributes = [
{
key1: "value1",
key2: "value2",
},
["BooleanAttribute1", "BooleanAttribute2"],
];
SetAttributes(sample);
链接到 Playground
英文:
Try to add some type casting and checking:
type BooleanAttributeName = string;
type PairedAttribute = {
[_: string]: string | number;
};
type CompositeAttribute = [
PairedAttribute,
BooleanAttributeName | BooleanAttributeName[]
];
type Attributes = CompositeAttribute | PairedAttribute;
function SetAttributes(attributes: Attributes) {
if (typeof attributes === 'object') {
const attr = attributes as PairedAttribute;
for (const key in attr) {
console.log(key, attr[key]);
}
}
if (attributes instanceof Array) {
const attr = attributes as CompositeAttribute;
attr.map((item) => {
if (typeof item === "object") {
const it = item as PairedAttribute;
for (const key in it) {
console.log(key, it[key]);
}
} else {
console.log(item);
}
});
}
}
let sample: Attributes = [
{
key1: "value1",
key2: "value2",
},
["BooleanAttribute1", "BooleanAttribute2"],
];
SetAttributes(sample);
Link to playground
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论