Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mfarhadattari/ts-intro
https://github.com/mfarhadattari/ts-intro
Last synced: 30 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/mfarhadattari/ts-intro
- Owner: mfarhadattari
- Created: 2023-08-19T08:26:30.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-08-20T12:34:11.000Z (over 1 year ago)
- Last Synced: 2023-08-20T14:01:22.141Z (over 1 year ago)
- Language: TypeScript
- Size: 16.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# TypeScript Introduction
TypeScript is a strongly typed programming language that builds on JavaScript.
## TypeScript Installation:
```
1. Install Node.js
2. Install TypeScript: npm i -g typescript
```## Commands:
```
tsc fileName.ts
tsc --init
tsc -w
```## Type Assignment
When creating a variable, there are two main ways TypeScript assigns a type:
```
- Explicit : Writing out the type
- Implicit : typeScript guess based on assignment
```## Explicit
```typescript
let name: string;
let age: number;
let isMarried: boolean;
```## Implicit
```typescript
let name = "Mohammad Farhad";
let age = 19;
let isMarried = false;
```## Data Type:
```
- boolean
- string
- number
- array
- tuples
- object
- enum
- bigNumber
- symbol
- any
- unknown
- never
- undefined
- null
```## Type Aliases & Interfaces
- Type Aliases allow defining types with a custom name which call an alias.
```typeScript
type Year = number
type CarObj = {
year: Year,
type: string,
model: string
}const car: CarObj = {
year: 2015,
type: "Toyato",
model: "Corolla"
}
```- Interfaces allow defining object types with a custom name.
```typeScript
interface Rectangle {
height: number,
width: number
}const rectangle: Rectangle = {
hight: 20,
width: 10
}// extending interface
interface ColoredRectangle extends Rectangle {
color: string
}const coloredRectangle: ColoredRectangle = {
height: 20,
width: 10,
color: "red"
};
```## Union type
- Explicit multiple data types
```typeScript
let id : string | number;
id = 5
id = "asd1"
```## Casting
- Casting with as
```typescript
let age: unknown = 19;
console.log(age as string);
```- casting with <>
```typeScript
let usename: unknown = "Mohammad Farhad";
console.log(usename);
```