- 对 MobX 使用 TypeScript 确保类型安全:
- 安装必要的类型定义:
- 对于 MobX 核心库和
mobx-react-lite,通常它们自带 TypeScript 类型定义。如果遇到类型错误,可能需要手动安装@types/mobx和@types/mobx-react-lite(如果它们不是内置的)。
- 对于 MobX 核心库和
- 使用装饰器(Decorators):
- MobX 推荐使用装饰器 (
@observable,@action,@computed) 来定义可观察状态、动作和计算值。在 TypeScript 中使用装饰器需要启用相应的编译器选项。 - 在
tsconfig.json中启用:{ "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true } } - 示例:
import { makeObservable, observable, action, computed } from 'mobx'; class UserStore { @observable name: string = 'John Doe'; @observable age: number = 30; constructor() { makeObservable(this); } @action setAge(newAge: number) { this.age = newAge; } @computed get isAdult() { return this.age >= 18; } }
- MobX 推荐使用装饰器 (
makeObservable或makeAutoObservable:- 对于 TypeScript 项目,推荐在类的
constructor中调用makeObservable(this)或makeAutoObservable(this)来明确地将类属性标记为可观察的。这比使用装饰器更现代,并且在某些环境中可能不需要额外的 Babel 配置。 - 示例(使用
makeObservable):import { makeObservable, observable, action, computed } from 'mobx'; class UserStore { name: string = 'John Doe'; age: number = 30; constructor() { makeObservable(this, { name: observable, age: observable, setAge: action, isAdult: computed, }); } setAge(newAge: number) { this.age = newAge; } get isAdult() { return this.age >= 18; } }
- 对于 TypeScript 项目,推荐在类的
- 类型推断与显式类型:
- TypeScript 能够很好地推断 MobX store 的类型,但在复杂场景下,显式地为属性、参数和返回值添加类型注解可以提高代码的可读性和健壮性。
- 接口和类型别名:
- 定义清晰的接口(
interface)或类型别名(type)来描述 MobX store 的状态结构和 action 的参数,有助于保持代码的一致性和可维护性。
- 定义清晰的接口(
- 与 React 组件结合:
- 在 React 函数式组件中使用
mobx-react-lite的observerHOC 或useObserverHook 时,确保组件 props 和 MobX store 访问的类型正确。
- 在 React 函数式组件中使用
- 安装必要的类型定义:
Mobx Typescript With Mobx
如何在 MobX 中使用 TypeScript 的说明
Mobx Typescript With Mobx by liaosw97 · f2b6b08
npx skillmds@latest add liaosw97/mobx-typescript-with-mobx File contents
---name: mobx-typescript-with-mobxdescription: 如何在 MobX 中使用 TypeScript 的说明---- **对 MobX 使用 TypeScript 确保类型安全**: - **安装必要的类型定义**: - 对于 MobX 核心库和 `mobx-react-lite`,通常它们自带 TypeScript 类型定义。如果遇到类型错误,可能需要手动安装 `@types/mobx` 和 `@types/mobx-react-lite`(如果它们不是内置的)。 - **使用装饰器(Decorators)**: - MobX 推荐使用装饰器 (`@observable`, `@action`, `@computed`) 来定义可观察状态、动作和计算值。在 TypeScript 中使用装饰器需要启用相应的编译器选项。 - 在 `tsconfig.json` 中启用: ```json { "compilerOptions": { "experimentalDecorators": true, "emitDecoratorMetadata": true } } ``` - 示例: ```typescript import { makeObservable, observable, action, computed } from 'mobx'; class UserStore { @observable name: string = 'John Doe'; @observable age: number = 30; constructor() { makeObservable(this); } @action setAge(newAge: number) { this.age = newAge; } @computed get isAdult() { return this.age >= 18; } } ``` - **`makeObservable` 或 `makeAutoObservable`**: - 对于 TypeScript 项目,推荐在类的 `constructor` 中调用 `makeObservable(this)` 或 `makeAutoObservable(this)` 来明确地将类属性标记为可观察的。这比使用装饰器更现代,并且在某些环境中可能不需要额外的 Babel 配置。 - 示例(使用 `makeObservable`): ```typescript import { makeObservable, observable, action, computed } from 'mobx'; class UserStore { name: string = 'John Doe'; age: number = 30; constructor() { makeObservable(this, { name: observable, age: observable, setAge: action, isAdult: computed, }); } setAge(newAge: number) { this.age = newAge; } get isAdult() { return this.age >= 18; } } ``` - **类型推断与显式类型**: - TypeScript 能够很好地推断 MobX store 的类型,但在复杂场景下,显式地为属性、参数和返回值添加类型注解可以提高代码的可读性和健壮性。 - **接口和类型别名**: - 定义清晰的接口(`interface`)或类型别名(`type`)来描述 MobX store 的状态结构和 action 的参数,有助于保持代码的一致性和可维护性。 - **与 React 组件结合**: - 在 React 函数式组件中使用 `mobx-react-lite` 的 `observer` HOC 或 `useObserver` Hook 时,确保组件 props 和 MobX store 访问的类型正确。
liaosw97/awesome-rules-skills/tree/main/plugins/mobx/skills/mobx-typescript-with-mobx commit f2b6b0866b
Frequently asked questions
Run npx skillmds@latest add liaosw97/mobx-typescript-with-mobx in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
如何在 MobX 中使用 TypeScript 的说明 It is listed under Coding & Dev Tools on SkillMD.
This skill has not completed SkillMD's automated safety review yet. Capability flags: docs only. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
liaosw97 (@liaosw97) published this skill. Their other Agent Skills are listed on their SkillMD profile.