How to convert SomeFunction () new syntax to TypeScript?

When I try to convert a simple JS file to TS with an implicit "any" disabled, I get this error:

error TS7009: the expression "new", in which there is no constructor signature, is implicitly of type "any".

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 

I do not see how to do this without converting the Logger function to a class.

Without implicit disconnections, the typescript compiler made any suggestions for improvement, so I would like to leave this option enabled.

Refresh . If I remove the β€œnew” from the Logger interface and then produce the result of a new Logger (...), try compiling it into a complete file, but in my smaller test For example, I keep getting the same error.

Update 2 Okay, I think it happens that error warnings disappear when a plugin that turns red highlights syntax errors and crashes. I think that this style of creating an object should be disabled if "implicit any" is disabled.

+4
source share
2 answers

Short bad answer

 interface IPerson { name: string; } var person = function(name: string) : void { this.name = name; } let funcPerson = <IPerson>(new (<any>(person("John Doe")))); 

This works and compiles with the noImplicitAny flag.

Longer answer

Just convert it to a class:

 class person { constructor(public name: string) { } } let classPerson = person("John Doe"); 

Compiled for:

 var person = (function () { function person(name) { this.name = name; } return person; })(); var classPerson = new person("Jane Doe"); 

This is IIFE , which in the simple case does not matter what has ever been compared with the implementation of var person = function... This is exactly the same.

Converting a new function to a class is useful. This makes the code readable, easier to reorganize, and easier to extend / modify in the future. It also has better type information (in this case you don't even need an interface).

In short, I cannot justify using a new version of functions over the class version of this solution. Both versions result in identical objects (see classPerson and funcPerson above).

Third option

If you have a working .js file that you want to use with typescript, you need to write a .d.ts file. In this case, you can do something like this:

person.js

 var person = function(name) { this.name = name; } 

person.d.ts

 interface PersonStatic { name:string; new(name: string) : PersonStatic; } declare var person: PersonStatic; 

When you use this, you can:

 /// <reference path="your.d.ts"/> var p = new person("Jane Doe"); 

and it will work.

In this case, the person.js file must be present at runtime for javascript to execute correctly. A file with the extension .d.ts above is a basic example, if you decide to go this route, I recommend that you first read about creating .d.ts files before downloading.

+5
source

Define the constructor structures of types and types. Interfaces will work fine.

 type PersonType = { name: string; greet: (otherPerson: PersonType) => string; selfIntroduce: () => string; } //ordinary functions are called with or without the "new" operator and //the constructor 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 } 

Define the function to be used as the constructor. this should be specified as the first parameter (starting with TS 2.0), so the compiler will not complain when the noImplicitThis flag is set to true.

 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)); 
+1
source

Source: https://habr.com/ru/post/1267188/


All Articles