如何将类型分配给字符串输入作为要推送的对象
How to assign type to string input as object to push
我有一个 forEach 接受一个字符串并推送一个对象。我该如何正确输入?
const descriptionText: string = this.args.post.description || '';
const descriptionParts: string[] = descriptionText.split(/\s+/g);
let parsedDescriptionParts: object[] = [];
descriptionParts.forEach((text: string) => {
let parts = {
text,
isLink: false
}
if (text.match('^(#|@)')) {
parts = {
text: `<span class=${this.boldClass}>${text}</span>`,
isLink: true
};
}
parsedDescriptionParts.pushObject(parts);
});
return parsedDescriptionParts;
在我看来,forEach 从来没有输出。
这是lib.es5.d.ts
中forEach
的定义
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
所以也许您的类型是:(string) => void
let descriptionParts: string[] = ['hello', '#goodbye']
let boldClass: string = 'abc'
let parsedDescriptionParts: object[] = descriptionParts
.map(text => text.match('^(#|@)')
? { text: `<span class=${boldClass}>${text}</span>`, isLink: true }
: { text, isLink: false }
)
console.log(parsedDescriptionParts)
我有一个 forEach 接受一个字符串并推送一个对象。我该如何正确输入?
const descriptionText: string = this.args.post.description || '';
const descriptionParts: string[] = descriptionText.split(/\s+/g);
let parsedDescriptionParts: object[] = [];
descriptionParts.forEach((text: string) => {
let parts = {
text,
isLink: false
}
if (text.match('^(#|@)')) {
parts = {
text: `<span class=${this.boldClass}>${text}</span>`,
isLink: true
};
}
parsedDescriptionParts.pushObject(parts);
});
return parsedDescriptionParts;
在我看来,forEach 从来没有输出。
这是lib.es5.d.ts
forEach
的定义
forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void;
所以也许您的类型是:(string) => void
let descriptionParts: string[] = ['hello', '#goodbye']
let boldClass: string = 'abc'
let parsedDescriptionParts: object[] = descriptionParts
.map(text => text.match('^(#|@)')
? { text: `<span class=${boldClass}>${text}</span>`, isLink: true }
: { text, isLink: false }
)
console.log(parsedDescriptionParts)