Skip to content
Advertisement

Tag: typescript

Casting a number to a string in TypeScript

Which is the the best way (if there is one) to cast from number to string in Typescript? In this case the compiler throws the error: Type ‘number’ is not assignable to type ‘string’ Because location.hash is a string. So which method is better? Answer “Casting” is different than conversion. In this case, window.location.hash will auto-convert a number to a

Typescript error : A ‘super’ call must be the first statement in the constructor when a class contains initialized properties

I have the following typescript errors in my project.. let me share a sample so you can see what am dealing with. This is the class that extends the controller class.. one among many others Now, if I initialize the merchandisingConstants before the super call like done above. I get the following error during gulp and my page does not

Do I have to reference TypeScript definition in every file?

Is there a way to tell TypeScript to use a certain file (or set of files) as a definition for everything compiled? My only alternative currently is to add something like this in every single TypeScript file (which seems clunky): Answer When using TypeScript’s internal module system, you can avoid having any <reference> tags at all in the code. I

Is there destructor in typeScript

Is there destructor in TypeScript? If not, how can I delete an object? I tried destructor() and ~ClassName() but it didn’t work. Answer JavaScript uses garbage collection to automatically delete objects when they are no longer referenced. There is no concept of destructors or finalizers. You can’t observe when an object is deleted by the garbage collector, nor is it

typescript – cloning object

I have a super class that is the parent (Entity) for many subclass (Customer, Product, ProductCategory…) I’m looking to clone dynamically an object that contains different sub objects in Typescript. In example : a Customer that has different Product who has a ProductCategory In order to clone the whole tree of object I created a function in Entity The new

How to call array reduce in TypeScript?

I am trying to find the largest item in an array. It was simple enough to solve using the straight-forward, simple, clean, elegant, fast method – iterating the array: But that’s not cool Then, rather than solving the problem using the easy way, i wanted to try to solve it using .reduce: And it’s great, and it compiles and all.

Is any JavaScript code a valid TypeScript code?

Currently I’ve started to learn TypeScript. From the documents I’ve studied for TypeScript, I saw some samples that pure JavaScript code could be compiled as TypeScript code. My question is: Is TypeScript language designed in a way that any JavaScript code will be a valid TypeScript code? i.e. is any .js file a valid .ts file? Answer Let’s assume valid

Advertisement