Typescript Flashcards
What is Typescript and why would we use it?
Its a Javascript Superset.
Its a language building up on Javascript. Adding new features on Javascript.
Typescript cant be executed by Typescript. Node.Js also cant execute typescript. Its a compiler you run on top of your code to which outputs Javascript.
It also adds Types to your Javascript with errors during compile time.
You would add Typescript in the case where you want to make sure that your code is typesafe.
In Javascript and HTML, what are the types of an input?
Its always a string that gets passed from the browser.
How do you check for types in Javascript?
typeof var1 === ‘number’.
what is === in Javascript?
It checks value and type.
Who made Typescript?
It was developed by and is maintained by a team at microsoft.
Why do you need Node.js for Typescript?
You need it to get NPM which is the tool that is used to install typescript.
How do you compile a typescript file to javascript?
you run the tsc command.
How do you tell typescript you will always find an element as an input element?
document.getElemebtById(“num1”) as HTMLInputElement;
as HTMLInputElement is called typecasting.
What is the syntax for specifying type on a function input?
function add (num1: number, num2: number)
What is Javascript for converting text to number
+ e.g. +textvalue.
what is defer in the script tag.
This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed, but before firing DOMContentLoaded . Scripts with the defer attribute will prevent the DOMContentLoaded event from firing until the script has loaded and finished evaluating.
With defer the browser can find the script slightly sooner than the preload scanner would, and then it can prioritize/download it in parallel while the DOM is parsing.
This means that the DOM can be completely parsed & domInteractive fires while the JS could still be getting downloaded, rather than waiting till after the JS comes back and gets parsed & executed.
In short: with defer on a script in the head, the JavaScript file will download in the background while the rest of the DOM is parsed and rendered. When you get to the bottom of the page, the JS file is already downloaded and ready to run.
If you have any noncritical JavaScript file, or any code that depends on the DOM being rendered to run, load it in the head with the defer attribute.
What is the basic setup process for a new project.
1 - Html file with script tag referening a javascript file.
2 - Scripts.ts file that will get compiled into js.
3 - NPM needs to be installed to manage the packages and dependencies required. Download it from the NPM website.
4 - Use NPM to install Typescript on your machine.
5 - you can compile typescript with the console. TSC app.tsx
6 - initialise the node package with node init. This creates the packages.json file which lists all the dependencies required to run the project. you can install depdencies as dev only etc..
How do you install and a dependency for developmet only?
npm install –save-dev
How do you run a script when the app starts?
add a start section in scripts the package.json file.
scripts:{
start: “lite-server”
}
How do you install a new dependency in your project?
With NPM.
For something that is dev only:
npm install –save-dev {tool name}
This will add a new line to “devDependencies” section in package.json.
How do you start a project?
NPM needs to start it since it knows the dependencies.
you type - npm start. (in your project folder)
What is node_modules folder and how do you create it?
Its a massive folder for all dependencies and their dependencies. Its created with the npm install command. you dont need it to be copied over.
What are the different number types in typescript?
There is only one number type. no integers or anything else.
How do you write a template literal string?
text