当试图将一个简单的JS文件转换为隐式“任意”禁用的TS时,我会得到以下错误:
error TS7009:缺少构造函数签名的“新”表达式隐式具有“任意”类型。
interface Logger {
    new ():any;
    //():any; // "Callable" syntax. same error.
    //new ():LoggerInstance; //Same error.
}
interface LoggerInstance {
}
function Logger(): void {
}
var defaultLogger: LoggerInstance = new Logger();//error TS7009
//var defaultLogger: LoggerInstance = <any>new Logger();//same error
//var defaultLogger: LoggerInstance = <LoggerInstance >new Logger();//same error在不将Logger函数转换为类的情况下,我看不出如何做到这一点。
在没有任何禁用的情况下,类型记录编译器提出了任何改进的建议,所以我想保留这个设置。
Update:如果我从Logger接口中删除" new“,然后强制转换新Logger(.)的结果,那么它将在完整的文件中编译,但是在我的小测试示例中,我继续得到相同的错误。
更新2好吧,我认为当插件出现红下划线语法错误崩溃时,错误警告就会消失。我认为,当禁用“隐式任何”时,必须禁止这种类型的对象创建。
发布于 2015-12-08 22:35:15
短小的坏答案
interface IPerson {
    name: string;
}
var person = function(name: string) : void {
    this.name = name;
}
let funcPerson = <IPerson>(new (<any>(person("John Doe"))));这与noImplicitAny标志一起工作和编译。
更长更好的答案
只需将其转换为一个类:
class person {
    constructor(public name: string) { }
}
let classPerson = person("John Doe");该文件汇编为:
var person = (function () {
    function person(name) {
        this.name = name;
    }
    return person;
})();
var classPerson = new person("Jane Doe");这是一个生平,在上面的简单案例中,它与var person = function...实现相比没有什么区别。他们是完全一样的东西。
将新函数转换为类只会有好处。它使代码具有可读性,更易于重构,将来更易于扩展/修改。它还有更好的类型信息(在这种情况下甚至不需要接口)。
简而言之,我没有理由在这个解决方案的类版本之上使用新的函数版本。这两个版本都会产生相同的对象(请参阅上面的classPerson和funcPerson )。
第三种选择--
如果您有一个要与类型记录一起使用的工作.js文件,那么一个选项是编写一个.d.ts文件。在这种情况下,您可以这样做:
person.js
var person = function(name) {
    this.name = name;
}person.d.ts
interface PersonStatic {
    name:string;
    new(name: string) : PersonStatic;
}
declare var person: PersonStatic;当您使用它时,您将能够做到:
/// <reference path="your.d.ts"/>
var p = new person("Jane Doe");而且会成功的。
在这种情况下,person.js文件必须在运行时出现,才能正确执行javascript。上面的.d.ts文件是一个基本的例子,如果您决定沿着这条路走下去,我建议在开始之前先阅读一下如何创建.d.ts文件。
发布于 2017-11-13 01:10:16
定义类型和类型的构造函数结构。接口可以正常工作。
type PersonType = {
    name: string;
    greet: (otherPerson: PersonType) => string;
    selfIntroduce: () => string;
}
//ordinary functions are called with or without the "new" operator and
//the constructor's type must indicate both signatures
type PersonTypeConstructor = {
    (this: PersonType, name: string): void; //<- "this" could be omitted here
    new(name: string): PersonType; // <- "this" must not appear here
}定义将用作构造函数的函数。必须将此指定为第一个参数(自TS2.0以来),以便编译器在设置noImplicitThis标志时不会抱怨。
const Person = (function Person(this: PersonType, name: string) { 
    this.name = name;
}) as PersonTypeConstructor; // <- cast the constructor here so no other cast will be necessary.
Person.prototype.greet = function (otherPerson: PersonType) {
    return `Hello, ${otherPerson.name}!`;
};
Person.prototype.selfIntroduce = function () {
    return `Hello, my name is ${this.name}.`;
};
let peggySue = new Person("Peggy Sue");
let maryAnn  = new Person("Mary Ann");
console.log(maryAnn.selfIntroduce());
console.log(peggySue.greet(maryAnn));https://stackoverflow.com/questions/34098999
复制相似问题