Learning Typescript and React in ts

news2024/10/7 6:45:33

目录

Basic typescript 

 What is typescript?

Configuring the TypeScript Compiler

Fundamental

build in types 

 TypeScript Simple Types

TypeScript Special Types 

Type: unknown

Type: never

Type: undefined & null

Arrays

Tuple 

Enums

functions

Object 

Type Aliases

Interfaces

Union | (OR)

Optional Chaining

Basic typescript 

学习视频来自:

https://www.youtube.com/watch?v=d56mG7DezGs

TypeScript Simple Types

 What is typescript?

TypeScript is a syntactic superset of JavaScript which adds static typing.

This basically means that TypeScript adds syntax on top of JavaScript, allowing developers to add types

左边不可以给同一变量赋值不同的type。右边可以赋值不同的type,当它使用Math.round()的时候,不会报错啊!这就是弊端。typescript就加入了static typing。

Benefits:

  • static typing
  • code completion
  • refactoring
  • shorthand notations

How to compile?

tsc index.ts

Configuring the TypeScript Compiler

terminal:

tsc --init

Create tsconfig.json file

configure json:

create src directory 

fix the path, uncomment the following thing

"rootDir": "./src",     
"outDir": "./dist",                                   /* Specify an output folder for all emitted files. */
"removeComments": true,  

Fundamental

build in types 

 TypeScript Simple Types

Explicit Type

let firstName: string = "Dylan";

Implicit Type

let firstName = "Dylan";

Note: Having TypeScript "guess" the type of a value is called infer.

TypeScript Special Types 

any会禁用类型检查:

let v: any = true;
v = "string"; // no error as it can be "any" type
Math.round(v); // no error as it can be "any" type

Type: unknown

跟any 有些像

Type: never

never effectively throws an error whenever it is defined

let x: never = true; // Error: Type 'boolean' is not assignable to type 'never'.

Type: undefined & null

undefined and null are types that refer to the JavaScript primitives undefined and null respectively.

let y: undefined = undefined;
let z: null = null;

Arrays

const names: string[] = [];
names.push("Dylan"); // no error

Tuple 

tuple is a typed array 

// define our tuple
let ourTuple: [number, boolean, string];

// initialize correctly
ourTuple = [5, false, 'Coding God was here'];

Enums

An enum is a special "class" that represents a group of constants (unchangeable variables).

By default, enums will initialize the first value to 0 and add 1 to each additional value:

enum CardinalDirections {
  North,
  East,
  South,
  West
}
let currentDirection = CardinalDirections.North;
// logs 0
console.log(currentDirection);
// throws error as 'North' is not a valid enum
currentDirection = 'North'; // Error: "North" is not assignable to type 'CardinalDirections'.

functions

TypeScript has a specific syntax for typing function parameters and return values.

you can define types of parameters and function 

function getTime(): number {
  return new Date().getTime();
}
function multiply(a: number, b: number) {
  return a * b;
}

Object 

const car: { type: string, model: string, year: number } = {
  type: "Toyota",
  model: "Corolla",
  year: 2009
};

Type Aliases

Type Aliases allow defining types with a custom name (an Alias).

Type Aliases can be used for primitives like string or more complex types such as objects and arrays:

reusable

type CarYear = number
type CarType = string
type CarModel = string
type Car = {
  year: CarYear,
  type: CarType,
  model: CarModel
}

const carYear: CarYear = 2001
const carType: CarType = "Toyota"
const carModel: CarModel = "Corolla"
const car: Car = {
  year: carYear,
  type: carType,
  model: carModel
};

Interfaces

Interfaces are similar to type aliases, except they only apply to object types.

interface Rectangle {
  height: number,
  width: number
}

const rectangle: Rectangle = {
  height: 20,
  width: 10
};

Union | (OR)

Using the | we are saying our parameter is a string or number:

function printStatusCode(code: string | number) {
  console.log(`My status code is ${code}.`)
}
printStatusCode(404);
printStatusCode('404');

Optional Chaining

Optional Chaining is a JavaScript feature that works well with TypeScript's null handling. It allows accessing properties on an object, that may or may not exist, with a compact syntax. It can be used with the ?. operator when accessing properties.

他就像个可有可无的东西,标注了,他可以有也可以没有

React in Ts

https://www.youtube.com/watch?v=jrKcJxF0lAU

补充:

因为js 和ts 太像了,所以我总是写错代码。不知道怎么应用比较好。然后问了colin,他说,interface常会被使用,就是用来定义type的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/390785.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Java集合专题

文章目录框架体系CollectionListArrayListLinkedListVectorSetHashSetLinkedHashSetTreeSetMapHashMapHashtableLinkedHashMapTreeMapPropertiesCollections框架体系 1、集合主要分了两组(单列集合,双列集合) 2、Collection接口有两个重要的子…

2.SpringSecurity认证

2.1登录校验流程 2.2认证原理 *源码流程: *自定义认证流程: *校验流程: *认证和校验连接: 2.3思路分析 *登录:

SQLI-Labs通关(2)5-7关

跟之前一样首先传参,然后查看注入点以及闭合 用and 11 and 12都没问题,接下来测试单引号 利用 and 12的时候会报错 利用order by来判断列数 得出一共三列 接下来就是联合查询 但是这个并不会回显 那么就利用盲注或者报错注入 在这里我们利用报错来测…

Vue3的生命周期函数

文章目录🌟 写在前面🌟 生命周期钩子函数🌟 组合式API生命周期🌟 写在最后🌟 写在前面 专栏介绍: 凉哥作为 Vue 的忠实 粉丝输出过大量的 Vue 文章,应粉丝要求开始更新 Vue3 的相关技术文章&am…

OPPO 数据恢复:如何从 OPPO 手机恢复已删除的文件?

Oppo 手机以其精美的外观和拍摄的精美照片和视频而闻名。如果您不小心丢失了 OPPO 手机中珍贵的照片、视频等重要文件,并且为如何找回而苦恼,那么您来对地方了。我们其实有很多OPPO数据恢复方案,现在最重要的是尽快尝试这些方法,防…

Git 相关内容

目录 Git 相关流程和常用命令 Git workflow Git hooks Git 相关流程和常用命令 Git远程操作详解 - 阮一峰的网络日志 Git 使用规范流程 - 阮一峰的网络日志 常用 Git 命令清单 - 阮一峰的网络日志 Git workflow 啥玩意: 就是一个工作流程。可以比喻成一个河流…

用逻辑回归制作评分卡

目录 一.评分卡 二.导库,获取数据 三.探索数据与数据预处理 1.去除重复值 2.填补缺失值 3.描述性统计处理异常值 4.为什么不统一量纲,也不标准化数据分布 5.样本不均衡问题 6.分训练集和测试集 三.分箱 1.分多少个箱子才合适 2.分箱要达成什么…

Antlr4: 为parser rule添加label

1. parser rule中的label 1.1 简介 Antrl4语法文件Calculator.g4,stat和expr两个parser rule含有多个rule element,我们这两个parse rule的每个rule element添加了Alternative labels(简称label) 按照Antlr4的语法规则&#xff…

2022年显卡性能跑分排名表

2022年显卡性能跑分排名表(数据来源于快科技)这个版本的电脑显卡跑分榜第一的是NVIDIA GeForce RTX 3090 Ti显卡。由于显卡跑分受不同的测试环境、不同的显卡驱动版本以及不同散热设计而有所不同,所以显卡跑分会一直变化。 前二十名的台式电…

Linux(进程概念详解)

进程是如今编程领域非常重要的一个概念,进程是比较抽象的,不容易直接理解。因为进程与操作系统息息相关,因此在介绍进程之前,笔者打算先简易讲一下操作系统的工作流程,理解操作系统是如何管理软件和硬件的,…

垃圾收集器和内存分配(第五章)

《实战Java虚拟机:JVM故障诊断与性能优化 (第2版)》 Java 平台,标准版热点虚拟机垃圾回收调优指南 垃圾收集器虽然看起来数量比较多,但其实总体逻辑都是因为硬件环境的升级而演化出来的产品,不同垃圾收集器的产生总体可以划分为几…

智能优化算法应用:基于蚁狮优化算法的工程优化案例-附代码

智能优化算法应用:基于蚁狮算法的工程优化案例 文章目录智能优化算法应用:基于蚁狮算法的工程优化案例1.蚁狮算法2.压力容器设计问题3.三杆桁架设计问题4.拉压弹簧设计问题5.Matlab代码6.python代码摘要:本文介绍利用蚁狮搜索算法&#xff0c…

191、【动态规划】AcWing —— 900. 整数划分:完全背包解法+加减1解法(C++版本)

题目描述 参考文章:900. 整数划分 解题思路 因为本题中规定了数字从大到小,其实也就是不论是1 2 1 4,还是2 1 1 4,都会被看作是2 1 1 4这一种情况,因此本题是在遍历中不考虑结果顺序。 背包问题中只需考虑…

AcWing:并查集

并查集理论基础并查集的作用是什么:将两个集合合并。询问两个元素是否在一个集合当中。如果不使用并查集,要完成上述两个操作,我们需要:创建一个数组来表示某个元素在某个集合之中,如belong[x] a,即x元素在…

0201基础-组件-React

1 组件和模块 1.1 模块 对外提供特定功能的js程序,一般就是一个js文件 为什么拆分模块呢?随着业务逻辑增加,代码越来越多,越来越复杂。作用:复用js,简化js,提高js运行效率 1.2 模块化 当应用…

用gdb.attach()在gdb下断点但没停下的情况及解决办法

在python中,如果导入了pwntools,就可以使用里面的gdb.attach(io)的命令来下断点。 但是这一次鼠鼠遇到了一个情况就是下了断点,但是仍然无法在断点处开始运行,奇奇怪怪。 这是我的攻击脚本 我们运行一下。 可以看到其实已经运行起…

计算机网络模型、协议

ARP(IP->MAC)RARP(MAC->IP)TFTPHTTPDHCPNATARP(IP->MAC) 主机建立自己的ARP缓冲区存ARP列表 广播ARP请求,单播ARP响应 RARP(MAC->IP) 用于无盘工作站&am…

Java分布式全局ID(一)

随着互联网的不断发展,互联网企业的业务在飞速变化,推动着系统架构也在不断地发生变化。 如今微服务技术越来越成熟,很多企业都采用微服务架构来支撑内部及对外的业务,尤其是在高 并发大流量的电商业务场景下,微服务…

[1.#]第一章 计算机系统概述——知识回顾

第一章 计算机系统概述 知识回顾 (对于考研408而言) 这个章节主要以选择题形式考察。 总的来说,这个章节考察的深度、难度不会太大。另外,这个章节的分值占比是比较低的。 不过,对第一章的学习,有助于我们…

使用 Sublime Text 4 优雅地写C++

使用 Sublime Text 4 优雅地写C 进入sublime官网下载sublime的安装包(当然也可以在官网下载页面下载portable版本,不过建议下载默认的setup版本) 双击安装包: 应该一会就下载完成了。 此时可以在应用列表看到sublime:…