Tag: 打字机

打字稿错误TS2339

我运行一个只有一个名为iterator的类,如下所示: Iterator.class.ts export class Iterator { private itr: any; private opt: any; private keys: any; private type: string; constructor(iterable, options) { this.itr = iterable; this.opt = options; this.keys = []; this.prepare(); } private prepare() { this.type = this.itr.constructor.name.toLowerCase().trim(); if (['array', 'object'].includes(this.type)) { this.keys = Object.keys(this.itr); } } } 我还在我的项目中使用-g标志和local来安装打字稿。 但每当我尝试编译我的iterator.class.ts文件到js文件,我遇到了下面的错误: iterator.class.ts(18,29):错误TS2339:属性'includes'在types'string []'上不存在。 这是我的tsconfig.json文件,我安装了我在这个文件中提到的所有要求,如: { "compilerOptions": […]

无法在Typescript中使用生成器编译asynchronous代码

上下文 在我进入Typescript的过程中,我被告知阻止调用不应该在asynchronous代码中完成 。 我也使用了生成器,因为它们使目录遍历变得容易并且避免了堆栈空间的耗尽。 但是,当我将asynchronous代码(在本例中为readdir )与生成器结合使用时,编译器会抱怨yield只能在生成器中使用 ,这让我认为编译器不能将闭包,生成器和asynchronous代码组合在一起一去。 function *yyyymmGenerator(dir: string, props: Props) { const fs = require("fs"); const yyyy = props.range.getUTCFullYear().toString(); const mm = props.range.getUTCMonth().toString(); const start = `${yyyy}-${mm}`; const files = fs.readdir(dir, function(err, files) { for (let i = 0; i < files.length; i++) { const file: string = files[i]; if (file.localeCompare(start) >= 0) […]

在TypeScript中使用枚举,并在每个枚举上使用方法

我有这个TypeScript文件: export type TSumanToString = () => string; export interface ISumanEvent { explanation: string, toString: TSumanToString } export interface ISumanEvents{ [key: string]: ISumanEvent } export const events: ISumanEvents = Object.freeze({ // runner events TEST_FILE_CHILD_PROCESS_EXITED: { explanation: 'runner is started, fires before any test child processes are started.', toString: makeToString('TEST_FILE_CHILD_PROCESS_EXITED') }, RUNNER_EXIT_CODE: { explanation: 'runner is […]

我怎样才能以组的方式调用一个asynchronous函数?

对不起,我可能无法清楚地描述这个问题。 我会尝试: 现在我有一个asynchronous函数,它接受数据并做一些事情,例如 function myFunction(num: number):Promise<void> { return new Promise((resolve) => { console.log(num); return; }); } 我想在一个组中打印5个数字(顺序无关紧要)。 重要的是,我想在前面的组完成后打印下5个数字。 例如: 1, 2, 5, 4, 3, 6, 9, 8, 7, 10 … is valid 7, 10, 1, 2, 3, 4, 5, 6, 8, 9 … is not valid 如果我必须使用这个函数,我怎么能做到这一点? 我必须确定这个function的前五个调用已经解决,然后调用下面五个函数的调用。 我知道这似乎很奇怪,我试图把我目前的问题抽象成这个数字问题。 感谢您的任何意见或想法。