Skip to content

Commit

Permalink
feat: ts re learn
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangtao committed Oct 9, 2024
1 parent addb318 commit f7771db
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/ts/geek/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function getData(id) {
return {
id: id,
name: 'jth',
age: 20
};
}
var a = 1;
console.log(getData(a));
18 changes: 18 additions & 0 deletions docs/ts/geek/1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

interface Person {
id: number
name: string
age: number
}

function getData(id: number): Person {
return {
id: id,
name: 'jth',
age: 20
}
}

let a: number = 1

console.log(getData(a))
7 changes: 7 additions & 0 deletions docs/ts/geek/2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
var a1 = Symbol();
var a2 = 1;
var a3 = '1';
var a4 = true;
var a5 = undefined;
var a6 = null;
var a7 = {};
48 changes: 48 additions & 0 deletions docs/ts/geek/2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
let a1: symbol = Symbol()
let a2: number = 1
let a3: string = '1'
let a4: boolean = true
let a5: undefined = undefined
let a6: null = null
let a7: object = {}

// 子类可以赋值父类
class StringEx extends String {
constructor(...args: any[]) {
super(...args)
}
}
// 包装器对象 new String new Object, 不能直接赋值给 JS 类型
let a8: String = new String('1')

a8 = new StringEx('2')

a8 = '3' // JS 类型可以赋值到包装器对象, 直至root对象



interface Point {
x: number
y: number
}

interface Point3D extends Point {
z: number
}

type point2d = {
x: number
y: number
}
type point3d = point2d & {
z: number
}

interface point3D {
radius: number
}

// 不被允许
type point3d = {
radius: number
} & Point
65 changes: 65 additions & 0 deletions docs/ts/geek/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
## L1

1. TS 主要作用
- 代码上显式标注类型, 让代码更加易读
- 开发和编译时, 通过类型推断和静态类型检查可以显著提高代码质量
- 发现潜在的编码风险, 提前暴露风险

2. TS 主要用法
- 编译制定文件 (tsc xx.ts)
- 使用 tsc 初始化 ts 工程

## L2

1. JS 的类型和 TS 的类型

JS 类型分为
- 引用类型: function object
- 值类型: symbol number boolean string
- 初始值: undefined null

> JS类型可以直接在 TS 中引用定义
子类可以赋值给父类
包装器对象 new String new Object, 不能直接赋值给 JS 类型

---
类型传递

字面类型 => 原始类型 => 包装类型 => 祖先类 -> 空接口/空对象 Empty {}

任意子类 => 父类p

'abc' => string => String => Object => Empty


2. TS 类型

- 基本类型
- Arrays
- any
- functions
- 参数
- 返回值

- Object Types
- Optional Properties
- Union Types
- 类型别名
- 接口
- 所有的接口都可以实现类型别名
- > 类型别名和接口之间的区别
类型别名和接口非常相似,在大多数情况下你可以在它们之间自由选择。 几乎所有的 interface 功能都可以在 type 中使用,关键区别在于不能重新开放类型以添加新的属性,而接口始终是可扩展的
- > 类型别名是不能添加新字段的
- Type Assertions
- as
- <HTMLCanvasElement>
- iteral types (字面类型)








50 changes: 50 additions & 0 deletions docs/ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions docs/ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "ts",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^22.7.5"
}
}
74 changes: 74 additions & 0 deletions docs/ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */
"module": "es2020", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
// "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
// "sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
// "outDir": "./", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "composite": true, /* Enable project compilation */
// "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
// "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */

/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
// "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */

/* Module Resolution Options */
// "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
// "typeRoots": [], /* List of folders to include type definitions from. */
// "types": [], /* Type declaration files to be included in compilation. */
// "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */

/* Source Map Options */
// "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */

/* Experimental Options */
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */

/* Advanced Options */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
},
"include": ["geek/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit f7771db

Please sign in to comment.