当前位置:网站首页>Getting started with typescript
Getting started with typescript
2022-07-05 06:26:00 【Heerey525】
1、 Premise
Install well node
, Then install it globally typescript
npm install typescript -g
2、 Debugging tools
Create a new one tsdemo.ts
file , Write any function
function fn() {
let web: string = "hello world";
console.log(web)
}
fn();
You can use commands , Convert to js file , And then run
tsc tsdemo.ts
node tsdemo.js
The operation above is a little more troublesome
Can install ts-node
, You can run it directly tsdemo.ts
npm install -D ts-node
After installation , If ts-node tsdemo.ts
If an error , Install again @types/node
npm install -D tslib @types/node
ts-node tsdemo.ts
3、 Basic type and usage
// string type
const str: string = 'hello world';
// object type
const obj: {
name: string, age: number } = {
name: ' The small white ',
age: 20
}
// Define... Through the interface objInterFace type , Realize reuse interface Annotate reusable types , Defined as a unified interface
interface objInterFace {
name: string;
age: number;
height?: number; // Optional value
[propName: string]: any; // Can contain any number of other attributes ( The property name is a string type , Attribute values are of any type )
skill(): string; // You can set the method , The return value is of string type
}
const obj2: objInterFace = {
name: ' The small white ',
age: 20,
other1: null,
skill() {
return ' Skill '
}
}
const obj3 = {
name: ' The small white ',
age: 20,
other2: ' Any value ',
skill() {
return ' Skill '
}
}
const fnInterface = ( e: objInterFace ) => {
console.log(' full name :', e.name, ' Age :', e.age, ' other :', e.other)
}
fnInterface(obj3)
// The content is string An array of types
const arr: string[] = ['1', '2'];
// The content is number An array of types
const arr1: number[] = [1, 2];
// The content is numbe perhaps stringr An array of types
const arr2: (number | string)[] = ['1', 2, '3'];
// The content is an array of objects
const arr3: {
name: string, age: number }[] = [
{
name: ' The small white ', age: 20 },
{
name: ' Little black ', age: 21 }
]
// Type the alias
type arr4Type = {
name: string, age: number }
const arr4: arr4Type[] = [
{
name: ' The small white ', age: 20 },
]
// class
class arr5Class {
name: string
age: number
}
const arr5: arr5Class[] = [
{
name: ' The small white ', age: 20 },
]
// Tuples
const tuple: [number, string, boolean] = [1, '2', true]
const tuple1: [number, string, boolean][] = [
[1, '2', true],
[9, '8', false]
]
// Type notes
let num: number;
num = 1;
// Type inference
let num2 = 1
// Regulations fn function , receive first and second Two parameters , The result is number type
function fn(first: number, second: number): number {
return first + second;
}
const fnResult = fn(1, 2)
// noReturn Function has no return value , add to void annotation
function noReturn(): void {
console.log(' No results returned ');
}
// Function arguments are objects
function fn1({
first, second}: {
first: number, second: number}): number {
return first + second;
}
const fnResult1 = fn1({
first: 1, second: 2 })
To be continued …
Reference material :
TypeScript Chinese net
TypeScript From getting started to mastering graphic and video tutorials - Free tutorial
边栏推荐
- 1.手动创建Oracle数据库
- 博弈论 AcWing 892. 台阶-Nim游戏
- __ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
- 【LeetCode】Easy | 20. Valid parentheses
- Leetcode-3: Longest substring without repeated characters
- C job interview - casting and comparing - C job interview - casting and comparing
- Redis-01.初识Redis
- [BMZCTF-pwn] ectf-2014 seddit
- Series of how MySQL works (VIII) 14 figures explain the atomicity of MySQL transactions and the principle of undo logging
- [leetcode] day94 reshape matrix
猜你喜欢
2.Oracle-数据文件的添加及管理
There are three kinds of SQL connections: internal connection, external connection and cross connection
LeetCode 0108. Convert an ordered array into a binary search tree - the median of the array is the root, and the left and right of the median are the left and right subtrees respectively
【LeetCode】Easy | 20. Valid parentheses
confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
2021apmcm post game Summary - edge detection
Leetcode-6111: spiral matrix IV
[moviepy] unable to find a solution for exe
5. Oracle tablespace
Game theory acwing 892 Steps Nim game
随机推荐
MPLS experiment
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
SQL三种连接:内连接、外连接、交叉连接
【LeetCode】Day95-有效的数独&矩阵置零
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
在新线程中使用Handler
5.Oracle-表空间
Redis-02.Redis命令
Game theory acwing 892 Steps Nim game
Nested method, calculation attribute is not applicable, use methods
Paper reading report
[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
International Open Source firmware Foundation (osff) organization
5. Oracle tablespace
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Sum of three terms (construction)
2048 project realization
Currently clicked button and current mouse coordinates in QT judgment interface
Leetcode stack related
7.Oracle-表结构