Last active
April 14, 2022 02:12
-
-
Save xiongchengqing/506e56a96aa08c89df623943e8b50abc to your computer and use it in GitHub Desktop.
typescript lessons
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 11. 元组转换为对象 | |
const tuple = ['tesla', 'model 3', 'model X', 'model Y'] as const | |
type TupleToObject<T extends readonly any[]> = {[P in T[number]]: P} | |
type result = TupleToObject<typeof tuple> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 同一类型的不同表述 | |
let scores: number[] = [1, 2, 3, 4, 99] | |
let anotherScores: Array<number> = [1, 3, 4, 6, 7] | |
scores = anotherScores // ✅ | |
// anotherScores = scores | |
/** | |
* A pure function that add two numbers | |
* | |
* @param {number} x adder param **one** | |
* @param {number} y adder param **two** | |
* | |
*/ | |
function add(x: number, y: number): number { | |
return x + y | |
} | |
add(1, 2) | |
// 接口,duck type | |
interface Employee { | |
title: string | |
name: string | |
age: number | |
} | |
let developer = { | |
title: 'Frontend-Developer', | |
name: 'xiongchengqing', | |
age: 30, | |
editor: 'VSCODE' | |
} | |
let juniorEmployee: Employee = developer // ✅ duck type | |
let seniorEmployee: Employee = { | |
title: 'Frontend-Developer', | |
name: 'jiangjun', | |
age: 30, | |
editor: 'WEBSTORM' | |
} // ❌ 直接将对象字面量赋值不行 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment