TypeScript Basics

0.0(0)
studied byStudied by 2 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/33

flashcard set

Earn XP

Description and Tags

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

34 Terms

1
New cards
Typescript is...
a superset of JS (extends JS)
the core/base JS syntax does not change, but adds more features to JS
2
New cards
Typescript adds this to JS
static typing
3
New cards
Code: Adding Type in TS
variable: type
(example: function add(a: number, b:number))
4
New cards
Why is adding types helpful?
catching errors before you test you code
5
New cards
Installing TS (locally & globally)
npm install typescript (-g installs globally)
6
New cards
Why do we need to compile TS to JS?
Typescript code does not run in the browserW
7
New cards
What happens when you invoke the TS invoker
-TS will be compiled to JS
- Errors will be notified during this process (potential code problems)
8
New cards
How to evoke the TS compiler (2)
-npx tsc
-npx tsc filename.ts
9
New cards
Event if you get an error in your code, this will still generate (TS invoker)
a JS file based on your TS file
10
New cards
Primitives in TS
number, string, boolean, null, undefined, symbols
11
New cards
these two primitives should be written in lowercase
string, number
12
New cards
why should string and number be written in lowercase when giving a variable a type
if you use caps you will be using the object Number and String instead of the primative
13
New cards
Code: Declare an array in TS
let hobbies: string[]
14
New cards
Code: declaring an object in TS
let person:{name: string; age:number;}
15
New cards
Type inference
by default TS tries to guess as many types as possible if if you dont explicitly write what type it is
16
New cards
Union Type
Type definition that allows more than one type. Connected together using pipes |
17
New cards
Features of Union Types
- You can have as many types as you want.
- can be used anywhere (arrays)
- allows more flexible values and types
18
New cards
Code: Union types
let course: string | number;
19
New cards
Type Aliases
Define your own base type and use that type alias instead of repeating the same type definitions.
The Type keyword is unique to TS (thrown out the window when compiled to JS)
20
New cards
Code: Type Aliases
type Person = {name:string' age: number};
let people: Person[];
21
New cards
Generics
Telling TS that the parameter type (and return type) is not any but the type of the parameter value. The T type is locked in.
22
New cards
Code: Generics Example
function functionName(p1: T[], p2: T)
23
New cards
Classes
Blueprints for objects you plan on installing. Supported by TS and modern JS
24
New cards
Class functions do not have this keyword in front of them
function
25
New cards
Something we can do in TS classes but not JS
define all properties & types in advance (not in the constructor)
26
New cards
Although it is common practice to use the constructor to define property values, you can also do this instead (TS Unique)
add accessor keywords in front of params in the constructor and instead leave the body of the function empty
27
New cards
Code: Using constructor to define property values
constructor(public p1: type, public p2: type, private p3:type){}
28
New cards
Interfaces
Object type definitions, only exists in TS and not vanilla JS (will not be compiled to JS)
29
New cards
When writing a method in an interface you
dont add actual code, just the type of method
30
New cards
Code: Interface method
methodName: (params) => type
31
New cards
Interfaces are an alternative to this keyword
typeB
32
New cards
Benefits of interfaces
- great for developing in large items
- forces us to add features to classes we define (good when developing with large teams)
33
New cards
Code: Configuring TS Compiler
npx tsc --init
34
New cards
what npx tsc --init does
-configures TS compiler
- adds ts to config.js file