小编Chr*_*odd的帖子

TypeScript类继承构造函数混淆

我正在阅读Eloquent Javascript这本书,我在章节结束时遇到了一些障碍.我很早就决定,我会主要使用TypeScript在vanilla JS之上解决这些练习,只是为了让自己接触到TS提供的额外功能.

完整的练习可以在这里找到:http://eloquentjavascript.net/06_object.html#h_nLNNevzcF7

在我看来,我本来应该扩展一个已经由本章作者定义的预先存在的类,我已经尽力在TypeScript中重写以利用类:

//from textbook.

function repeat(string: string, times: number): string {
    var result = '';
    for (var i = 0; i < times; i++)
        result += string;
    return result;
}

//inferred from textbook.

class TextCell {
    text: any;
    constructor(text: string) {
        this.text = text.split('');
    }
    minWidth(): number {
        return this.text.reduce((width: number, line: any) => Math.max(width, line.length), 0);
    }
    minHeight(): number {
        return this.text.length;
    }
    draw(width: number, height: number) : any[]{
        var result: any[] = …
Run Code Online (Sandbox Code Playgroud)

javascript oop inheritance class typescript

7
推荐指数
1
解决办法
1万
查看次数

使用ts-node进行测试时,TypeScript Express Api类不是构造函数

我已经使用typescript类设置了一个快速应用程序,并遇到了一个奇怪的问题.所有的测试都已经过去了,今天当我去更新一些路线时,我的测试不再运行了.当我运行我的测试脚本时,我在控制台中收到此错误消息:

$ mocha -c --reporter spec --compilers ts:ts-node/register ./test/*.test.ts --
timeout 20000

/Users/christiantodd/Development/projects/bby-react-api/src/index.ts:8
const app: Api = new Api();
             ^
TypeError: Api_1.default is not a constructor
at Object.<anonymous> (/Users/christiantodd/Development/projects/bby-react-api/src/index.ts:8:18)
at Module._compile (module.js:635:30)
at Module.m._compile (/Users/christiantodd/Development/projects/bby-react-api/node_modules/ts-node/src/index.ts:392:23)
at Module._extensions..js (module.js:646:10)
at Object.require.extensions.(anonymous function) [as .ts] (/Users/christiantodd/Development/projects/bby-react-api/node_modules/ts-node/src/index.ts:395:12)
Run Code Online (Sandbox Code Playgroud)

我的Api.ts文件如下:

import * as bodyParser from 'body-parser';
import * as express from 'express';
import * as expressValidator from 'express-validator';
import * as helmet from 'helmet';
import * as morgan from 'morgan';
import * as …
Run Code Online (Sandbox Code Playgroud)

mocha.js node.js express typescript ts-node

2
推荐指数
1
解决办法
584
查看次数