如何解决 TypeScript 中的错误?

huangapple go评论107阅读模式
英文:

How can I solve errors in TypeScript?

问题

  1. 我是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);

  1. 我尝试使用了[官方TypeScript文档](https://www.typescriptlang.org/docs/handbook/2/narrowing.html)中的一些缩小类型的概念,但仍然遇到了这些问题。
  2. 我想知道如何解决这个问题的解决方案。
英文:

I'm new to TypeScript. Below is my code with errors mentioned in comments. What is going wrong?

  1. type BooleanAttributeName = string;
  2. type PairedAttribute = {
  3. [_: string]: string | number;
  4. };
  5. type CompositeAttribute = [
  6. PairedAttribute,
  7. BooleanAttributeName | BooleanAttributeName[]
  8. ];
  9. type Attributes = CompositeAttribute | PairedAttribute;
  10. function SetAttributes(attributes: Attributes) {
  11. if (attributes.length === undefined) {
  12. for (const key in attributes) {
  13. 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'.*/
  14. }
  15. }
  16. 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.*/
  17. if (item.length === undefined) {
  18. for (const key in item) {
  19. console.log(key, item[key]);
  20. }
  21. } else if (typeof item === "object") {
  22. item.map((BooleanAttributeName) => { /*Parameter 'BooleanAttributeName' implicitly has an 'any' type.*/
  23. console.log(BooleanAttributeName);
  24. });
  25. } else {
  26. console.log(item);
  27. }
  28. });
  29. }
  30. let sample: Attributes = [
  31. {
  32. key1: "value1",
  33. key2: "value2",
  34. },
  35. ["BooleanAttribute1", "BooleanAttribute2"],
  36. ];
  37. 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

尝试添加一些类型转换和检查:

  1. type BooleanAttributeName = string;
  2. type PairedAttribute = {
  3. [_: string]: string | number;
  4. };
  5. type CompositeAttribute = [
  6. PairedAttribute,
  7. BooleanAttributeName | BooleanAttributeName[]
  8. ];
  9. type Attributes = CompositeAttribute | PairedAttribute;
  10. function SetAttributes(attributes: Attributes) {
  11. if (typeof attributes === 'object') {
  12. const attr = attributes as PairedAttribute;
  13. for (const key in attr) {
  14. console.log(key, attr[key]);
  15. }
  16. }
  17. if (attributes instanceof Array) {
  18. const attr = attributes as CompositeAttribute;
  19. attr.map((item) => {
  20. if (typeof item === "object") {
  21. const it = item as PairedAttribute;
  22. for (const key in it) {
  23. console.log(key, it[key]);
  24. }
  25. } else {
  26. console.log(item);
  27. }
  28. });
  29. }
  30. }
  31. let sample: Attributes = [
  32. {
  33. key1: "value1",
  34. key2: "value2",
  35. },
  36. ["BooleanAttribute1", "BooleanAttribute2"],
  37. ];
  38. SetAttributes(sample);

链接到 Playground

英文:

Try to add some type casting and checking:

  1. type BooleanAttributeName = string;
  2. type PairedAttribute = {
  3. [_: string]: string | number;
  4. };
  5. type CompositeAttribute = [
  6. PairedAttribute,
  7. BooleanAttributeName | BooleanAttributeName[]
  8. ];
  9. type Attributes = CompositeAttribute | PairedAttribute;
  10. function SetAttributes(attributes: Attributes) {
  11. if (typeof attributes === 'object') {
  12. const attr = attributes as PairedAttribute;
  13. for (const key in attr) {
  14. console.log(key, attr[key]);
  15. }
  16. }
  17. if (attributes instanceof Array) {
  18. const attr = attributes as CompositeAttribute;
  19. attr.map((item) => {
  20. if (typeof item === "object") {
  21. const it = item as PairedAttribute;
  22. for (const key in it) {
  23. console.log(key, it[key]);
  24. }
  25. } else {
  26. console.log(item);
  27. }
  28. });
  29. }
  30. }
  31. let sample: Attributes = [
  32. {
  33. key1: "value1",
  34. key2: "value2",
  35. },
  36. ["BooleanAttribute1", "BooleanAttribute2"],
  37. ];
  38. SetAttributes(sample);

Link to playground

huangapple
  • 本文由 发表于 2023年1月9日 15:50:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定