Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is TypeScript's purpose to be used for manipulating the DOM like JavaScript is?

Every TypeScript example and tutorial I have found thus far is about language features, Static typing, Visual Studio, etc. I cannot find anything to tell me how I should be using it with reference to JavaScript and the DOM.

My understanding it you can use TypeScript for the DOM. But, I never see anyone using it for that. I have a set of JavaScript IIFE objects, and I want to convert them, but as these manipulate the DOM, I'm not sure what part TypeScript plays here.

As it concerns manipulating the DOM, should I be using TypeScript for this? Not can I. Is it expected that I should, given I am using TypeScript in a new Web Application Project.

I do not know this answers it, What is TypeScript and why would I use it in place of JavaScript?

Edit: I understand what TypeScript is and does. My question is more why don't I see more people using it for the DOM? Is it not supposed to be used that way with the DOM, even though it can be used that way?

like image 587
johnny Avatar asked Apr 04 '17 14:04

johnny


2 Answers

TypeScript can be used for anything JavaScript can be used for. It's JavaScript plus a layer of type information (and usually adopts newer JavaScript features fairly quickly), which is then compiled to JavaScript for deployment and use in the wild.

When using TypeScript to manipulate the DOM, you'll need type information for the DOM; see this question and its answers for more about getting DOM type information into your project.

like image 142
T.J. Crowder Avatar answered Oct 07 '22 15:10

T.J. Crowder


Typescript is excelent for DOM manipulation. You get a great amount of compiler support during development time thanks to types, and save a lot of bugs hard to discover only with Javascript.

Here is a small example of dom manipulation (look at the class definition):

Link to example

This is part of a Angular Library of UI Components I have made. You can take a look here:

Material Light. UI Components for Angular 4

like image 26
Yago Lopez Avatar answered Oct 07 '22 17:10

Yago Lopez