Skip to content

Commit

Permalink
Merge pull request #23 from zhangzhuang15/dev
Browse files Browse the repository at this point in the history
add more ts types content related with `extends` `infer`;
  • Loading branch information
zhangzhuang15 authored Feb 6, 2024
2 parents 9568de7 + 1867d20 commit df80de2
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
51 changes: 51 additions & 0 deletions docs/frontend/ts-type.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ type T<M> = M extends string ? [M] : never
// ('a' extends string ? ['a'] : never) |
// ('b' extends string ? ['b'] : never)
type K = T<'a' | 'b'>

// 为了防止映射,可以将 extends 前后倒转
type P<M> = string extends M ? [M] : never

// Q = ['a' | 'b']
type Q = P<'a' | 'b'>

// 或者利用元组
type H<M> = [M] extends [string] ? [M]: never

// R = ['a' | 'b']
type R = H<'a' | 'b'>
```
### 联合类型和联合类型的交集、差集
Expand Down Expand Up @@ -162,6 +174,30 @@ type D<T> = {[k in keyof T as k extends string ? k : never]: T[k] }
type E = D<A>
```
## 交叉类型
### 合并为一个interface
```ts
type A = {
name: string;
}

type B = {
age: number;
}

// C = { name: string } & { age: number }
type C = A & B;

// D = { name: string; age: number}
type D = Omit<A & B, never>;


type ToObj<T> = {[k in keyof T]: T[k]}

// E = { name: string; age: number}
type E = ToObj<A & B>;
```

## 一些extends
```ts
// A = 1
Expand Down Expand Up @@ -229,6 +265,9 @@ type R6 = H<A>

// R7 = ['1', 100]
type R7 = I<A>

// R8 = never
type R8 = H<[]>
```
### 推断字符串
```ts
Expand Down Expand Up @@ -269,6 +308,10 @@ type R8 = H<'helloaworld'>

// R9 = 'hello_a' | 'hello_b'
type R9 = I<'helloaworld'>

// R10 = never
// 这个特别容易以为是 R10 = ''
type R10 = A<''>
```
### 推断函数参数
Expand Down Expand Up @@ -323,4 +366,12 @@ type Filer<T, P = PropertyKey> = {

// c = { hello(): void }
type c = Filter<A>
```
## 实际参数少于范型定义的参数
```ts
// 方法很简单,给出一个默认类型值即可
type T<P, Q extends string = ''> = P

type C = T<string>
```
140 changes: 140 additions & 0 deletions docs/rust/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,145 @@ fn main() {
总之,模式匹配会不会造成所有权变更取决于模式匹配的对象和结果。如果对象和结果都是值类型,那么就会发生移动语义;如果对象和结果都是引用类型,那么就会发生借用语义;如果对象和结果的类型不一致,那么就会根据具体情况进行隐式转换或报错。


## mod的可见性规则?
```rust
// src/a.rs
mod b;

// 访问不了
b::c::ok();

// 访问不了
b::c::no();

// 可以访问
b::d::hello();

// 访问不了
b::d::world();
```

```rust
// src/a/b.rs
mod c;
pub mod d;

// 可以访问
c::ok();

// 访问不了
c::no();

// 可以访问
d::hello();

// 访问不了
d::world();
```

```rust
// src/a/b/c.rs
pub fn ok() {}
fn no() {}
```
```rust
// src/a/b/d.rs
pub fn hello() {}
fn world() {}
```

再来看看 struct 的可见性:
```rust
// src/a.rs
mod b;

// 出错
// XiaoMing 不可见,无法访问
b::XiaoMing {
name: "xiao_ming".to_string(),
}

// 出错
// name 不可见,无法访问
b::XiaoHong {
name: "xiao_hong".to_string(),
}

// 没有错误,正常运行
b::XiaoHai {
name: "xiao_hai".to_string(),
}

// 出错
// XiaoTong.0 不可见,无法访问
b::XiaoTong(1);

// 出错
// init 不可见,无法访问
b::XiaoHong::init();

// 没有错误,正常运行
b::XiaoHong::new();
```

```rust
// src/a/b.rs
struct XiaoMing {
name: String,
}

pub struct XiaoHong {
name: String,
}

pub struct XiaoHai {
pub name: String,
}

pub struct XiaoTong(i32);

// 仅仅在当前crate范围可见,别的crate引用不了
pub(crate) struct XiaoNao {}

impl XiaoHong {
fn init() -> Self {
XiaoHong {
name: "xiao_hong".to_string(),
}
}

pub fn new() -> Self {
XiaoHong {
name: "xiao_hong".to_string(),
}
}
}
```

再看看 struct 在定义处的可见性:
```rust
// src/a.rs

struct XiaoMing(i32);
pub struct XiaoHong(i32);

fn hello() {
// 正常运行
let m = XiaoMing(1);

// 出错
// world可见,ok不可见
world::ok();
}

mod world {
use super::XiaoMing;
fn ok() {
// 正常运行
let m = XiaoMing(1);
}

}
```

<Giscus />

0 comments on commit df80de2

Please sign in to comment.