There are two js files (ts-> js). Connected in html. In one class, in another. In the second file, the functions from the first one are used, but the taipa compiler considers them to be undefined. It works due to the fact that when compiling it does not touch their names and js clings them from the global scope. But still unpleasant, I want this kind of warning not to be. I will specify: I write on the client. Not node first file

/// <reference path="classes.d.ts" /> let digit = new Digit(number, cur_scale); // cannot find name Digit 

classes.ts

 class Digit { constructor(number, scale) {} } 

classes.d.ts

 export class Digit {} 
  • it is worth adding a minimal reproducible example - Grundy
  • @Grundy added. Included his pathetic attempts at determining through .d.ts. - Vyacheslav Potseluyko
  • MB I'm wrong, however 1) in what order do you have the files connected? 2) Is the conversion of ts to js made and how do you do it? - alexoander
  • @alexoander, you did not understand the question. This code works. The task to remove typescript scripts. Transformation in js is done by php storm - Vyacheslav Potseluyko
  • What version of typescript? - Grundy

2 answers 2

Interface:

 interface Digit { constructor(number: number, scale: number): void; } 

Announcement:

 declare class Digit { public constructor(number: number, scale: number): void; } 

You can choose from. But the interface can be said to be a simplified type of object, but it can be inherited.

The difference between the interface and declare on the English-language SO is also perfectly described.

UPD. For example, you can declare a class type (it does not cause fatal errors for me if the name of the interface or declare is the same as for some class)

my.d.ts

 declare class Digit { public constructor (number: number, scale: number); public testFunc (a: string): number; } 

fullfile.ts

 /// <reference path="../typings/my.d.ts" /> class Digit implements Digit { constructor (number: number, scale: number) { console.log(`CONSTRUCTOR ${number} ${scale}`); } testFunc (a: string): number { return 54; } } const hello = new Digit(2, 4).testFunc("5"); 

Those. You declare what methods you will use, so first always make the narrative part of a class / object / function.

Those. here it will swear if you do not declare the testFunc method in your class, and it will be in the declaration.

Or, if you declare something, for example, you can say that the variable will be of this type, then you need to write:

 const myValue: Digit = ... 
  • 1) the code is incorrect, does not answer the question. What does it have to do with interfaces? Moreover, if you write it in one file, everything will be highlighted with fatals. - Vyacheslav Potseluyko
  • Please re-read the question: how to make a class from one file visible in another? - Vyacheslav Potseluyko
  • but in more detail what fatals? And you have to choose one of the two, not both. You must not make it visible, you must declare its type, and connect it to the place where you use it. The interpreter will already know that this class from this interface is either declared this way and with such methods and properties. Or, in the build settings, specify what is coming from (I'm talking about types). - Vasily Barbashev
  • @ Vasily Barabashev, fatal, that the interface and class names should not be the same. You specify what and where should be, and what it was :). I solved my problem, wrote an answer. If you tell me about drugs in more detail what they meant, I will be terribly grateful. I 'm learning :) - Vyacheslav Potseluyko

The first file, worker.ts:

 /// <reference path="classes.ts" /> let digit = new Digit(number, cur_scale); 

The second classes.ts file:

 class Digit { constructor(number, scale) {} } 

In the first file, the class from the second is correctly defined.

  • mine you confuse something, typescript should not work that way. You must provide references to "help" data for the typescript, type declarations, etc. and so on What you have done is not the right approach. Describe classes.d.ts with a class type (can have any names) and inherit it, then everything will work through the class Digit implemets InterfaceForDigit - Vasily Barbashev
  • Now I will expand the service, write an example) - Vasily Barbashev
  • Yes please :) - Vyacheslav Potseluyko
  • one
    He added a bit, checked everything, everything worked for me locally in the Webstorm - Vasily Barbashev
  • @ Vasily Barbashev, and if I want to keep dearing of the class and its challenge in different classes - how then? Thank you - Vyacheslav Potseluyko