Live Note

Remain optimistic

Introduction

1
2
3
4
5
6
7
8
9
interface A {
text: string
}

class B {
text: string
}

let a: A = new B() // no error

At least same member

1
2
3
4
5
6
7
8
9
10
11
interface A {
text: string
}

function foo(obj: { text: string }): string {
return obj.text
}

let obj = { text: "some text", num: 3 }
let a: A = obj // success
foo(obj) // success
Read more »

基本类型

  • boolean:
    1
    let done: boolean = true
  • number:
    1
    let count: number = 20
  • string:
    1
    let name = "Edward Wang"
  • array:
    1
    2
    let list1: number[] = [1, 2, 3]
    let list2: Array<number> = [1, 2, 3]
  • tuple:
    1
    let x: [string, number] = ["test", 2]
  • enum:
    1
    2
    3
    4
    5
    6
    7
    enum NUMBER {
    ONE,
    TWO,
    THREE,
    }
    let a = NUMBER.TWO
    let title: string = NUMBER[3]
Read more »

1
2
3
4
5
6
7
8
9
10
const enum Foo {
AA = "aa",
BB = "bb",
}

type B = keyof typeof Foo // 'AA' | 'BB'

// Template Literal Types
type C = `${keyof { [x in Foo]: string }}`
// 'aa' | 'bb'

缘由

公司的 PC 没有音卡。导致耳机不能播放。

使用 pavucontrol 输出为模拟信号

刚开始总报找不到这个 package。
换了官方源也没用。
后来才发现需要打开开源 package 安装。。。software & updates > ubuntu software > open-source software

Interface

1
2
3
4
5
6
7
8
9
10
interface labelValue {
label: string
}

function printLabel(obj: labelValue): void {
console.log(obj.label)
}

let obj = { size: 10, label: "some text" }
printLabel(obj)

Optional Properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
interface SquareConfig {
color?: string
width?: number
}

function createSquare(config: SquareConfig): { color: string; area: number } {
let defaultSquare = { color: "White", area: 200 }
if (config.color) defaultSquare.color = config.color
if (config.width) defaultSquare.area = config.width ** 2

return defaultSquare
}

console.log(createSquare({ color: "Black", width: 30 }))

Readonly Properties

1
2
3
4
5
6
7
interface Point {
readonly x: number
readonly y: number
}

let readOnlyArray: ReadonlyArray<number> = [1, 2, 3, 4]
let a: number[] = readOnlyArray as number[] // readonly array assignment to ordinary array

Excess Property Checks

1
2
3
4
5
6
7
interface SquareConfig {
color?: string
width?: number
[propName: string]: any
}

let newSquare = createSquare({ width: 100, opacity: 0.5 } as SquareConfig)
Read more »