TS(一)Types

翻译自: https://ts.chibicode.com/todo
自学记录!!

分为三部分:

Section 1: Types, Read-only Properties, and Mapped Types
Section 2: Array Types, Literal Types, and Intersection Types
Section 3: Union Types and Optional Properties

Section 1

type Todo = {
  readonly id: number
  readonly text: string
  readonly done: boolean
}

function toggleTodo(todo: Todo): Todo {
  return {
    // This line was missing
    id: todo.id,
    text: todo.text,
    done: !todo.done
  }
}

除了挨个加readonly, 还可以像以下这样, 是每一个都是 只读 属性

type Foo = {
  bar: number
}
type ReadonlyFoo = Readonly<Foo>
// ReadonlyFoo is { readonly bar: number }

在这里插入图片描述
所以以上也可以写成

// Readonly<...> makes each property readonly
type Todo = Readonly<{
  id: number
  text: string
  done: boolean
}>

Section 2: Array Types, Literal Types, and Intersection Types

全选功能:

type Todo = Readonly<{
  id: number
  text: string
  done: boolean
}>

type CompletedTodo = Readonly<{
  id: number
  text: string
  done: true  // You can use exact values when specifying a type. This is called literal types.
}>

function completeAll(
  todos: readonly Todo[]
): CompletedTodo[] {
   return todos.map(todo => ({
    ...todo,
    done: true
  }))
}

Intersection Types:

type A = { a: number }
type B = { b: string }
// This intersection type…
type AandB = A & B
// …is equivalent to:
type AandB = {
  a: number
  b: string
}

如果用相同属性,会覆盖

// They booth have a property foo,
// but B’s foo (true) is
// more specific than A’s foo (boolean)
type A = { foo: boolean }
type B = { foo: true }
// This intersection type…
type AandB = A & B
// …is equivalent to:
type AandB = { foo: true }

所以 CompletedTodo 可以改写为:

type Todo = Readonly<{
  id: number
  text: string
  done: boolean
}>
// Override the done property of Todo
type CompletedTodo = Todo & {
  readonly done: true
}

Section 3: Union Types and Optional Properties

Union Types :

// Creates a union type of number and string
type Foo = number | string
// You can assign either a number or a string
// variable to Foo. So these will both compile:
const a: Foo = 1
const b: Foo = 'hello'
type Place = 'home' | 'work' | { custom: string }type Todo = Readonly<{
  id: number
  text: string
  done: boolean
  place: Place
}>

Optional Properties: 就是加一个? 表示非必须

type Foo = {
  // bar is an optional property because of "?"
  bar?: number
}
// These will both compile:
// bar can be present or missing
const a: Foo = {}
const b: Foo = { bar: 1 }
type Place = 'home' | 'work' | { custom: string }
type Todo = Readonly<{
  id: number
  text: string
  done: boolean
  // place is optional
  place?: Place
}>
### 如何在 TypeScript 中引入个类型 在 TypeScript 中,可以通过 `import` 语句来引入其他模块中的类型定义。需要注意的是,这种方式并不等同于 JavaScript 的动态导入(Dynamic Imports),而是 TypeScript 提供的种静态分析特性[^1]。 以下是几种常见的类型导入方式: #### 1. 使用命名导入 如果目标文件导出了特定的类型,则可以使用命名导入语法将其引入到当前文件中。 ```typescript // 假设有个名为 'MyType' 的类型存在于 './types' 文件中 import { MyType } from './types'; const example: MyType = { // 初始化对象属性... }; ``` #### 2. 使用默认导入 当目标模块仅暴露了个默认类型的导出时,可采用如下形式: ```typescript // 如果 ./default-type.ts 导出了个默认类型 DefaultType import DefaultType from './default-type'; function handleData(data: DefaultType): void { console.log(data); } ``` #### 3. 类型只读取 (Type-only imports) 有时只需要借用某个外部声明的结构而不实际运行其逻辑,在这种情况下推荐使用专门针对类型的导入方法——即通过 `type` 关键字限定范围内的操作不会影响最终编译后的代码体积。 ```typescript import type { SomeInterface } from './interfaces'; // 或者对于整个命名空间的情况 import type * as Types from './all-types'; let value: Types.SomeOtherType; ``` 以上就是关于如何正确地按照需求场景选取适合自己的 Type Importing 方法介绍。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值