@epic-web/invatant (原始内容存档于2012-12-04)
当事情不对时就抛出错误.特殊情况下投出错误(和答复)的安全设施类型 以宣示方式出现的情况.
npm install @epic-web/invariant
问题
您的申请有边界 。 网络请求/答复,文件系统读取, 包括: 当你用这些界限工作的时候,你需要能够处理 即使在 TypeScript 中也可能发生错误.
TypeScript 通常会让这些边界更加明显,因为它 不喜欢不知道是什么类型的东西。 例如:
const formData = new FormData(formElement)
const name = await formData.get('name')
// name is `File | string | null`
通常使用一个适当的解析库 来应对这样的情况, 但对于简单的案件, 往往会觉得过度杀戮。 但你不想这样 忽略 TypeScript 是因为:
TypeScript 是那个残忍的诚实朋友 你忍受,因为他们拯救 你犯过可怕的错误 @ kentcdods (英语)
你检查一下:
const formData = new FormData(formElement)
const name = await formData.get('name')
// name is `File | string | null`
if (typeof name !== 'string') {
throw new Error('Name must be a string')
}
// now name is `string` (and TypeScript knows it too)
你在这里抛出一个描述错误是好的,因为它只是 非常喜欢 可能性 这种事会发生的 即使你不知道该怎么做 说到这里.
这没什么大不了的 但有一小块锅炉板
避开。 特别是当你发现自己在代码库里做这一切时 这个
问题是 @epic-web/invariant 解说.
解决办法
这是上面的线索:
const formData = new FormData(formElement)
const name = await formData.get('name')
// name is `File | string | null`
- if (typeof name !== 'string') {
- throw new Error('Name must be a string')
- }
+ invariant(typeof name === 'string', 'Name must be a string')
// now name is `string` (and TypeScript knows it too)
很简单 但老实说,读起来更好,它扔一个特殊的
InvariantError 反对将其与其他类型的错误区分开来,我们
还有一个有用的工具来扔 Response 对象代替 Error
方便对象
在重混合中。 。 。 .
使用量
invariant
这个 invariant 函数用来断言一个条件是真的。 如果
条件不正确, 它会与所提供消息发生错误 .
基本用途
import { invariant } from '@epic-web/invariant'
const creature = { name: 'Dragon', type: 'Fire' }
invariant(creature.name === 'Dragon', 'Creature must be a Dragon')
对虚假条件提出回应
import { invariant } from '@epic-web/invariant'
const creature = { name: 'Unicorn', type: 'Magic' }
invariant(creature.type === 'Fire', 'Creature must be of type Fire')
// Throws: InvariantError: Creature must be of type Fire
使用回调错误消息
import { invariantResponse } from '@epic-web/invariant'
const creature = { name: 'Elf', type: 'Forest' }
invariant(creature.type === 'Water', () => 'Creature must be of type Water')
// Throws: InvariantError: Creature must be of type Water
invariantResponse
这个 invariantResponse 函数工作类似 invariant,但改为
抛出一个 InvariantError,它会扔出一个响应对象.
基本用途
import { invariantResponse } from '@epic-web/invariant'
const creature = { name: 'Phoenix', type: 'Fire' }
invariantResponse(creature.type === 'Fire', 'Creature must be of type Fire')
对虚假条件提出回应
import { invariantResponse } from '@epic-web/invariant'
const creature = { name: 'Griffin', type: 'Air' }
invariantResponse(creature.type === 'Water', 'Creature must be of type Water')
// Throws: Response { status: 400, body: 'Creature must be of type Water' }
如果有400个响应状态默认( Bad Request), 但你会找到如何改变 下面那个.
使用回调响应信件
import { invariantResponse } from '@epic-web/invariant'
const creature = { name: 'Mermaid', type: 'Water' }
invariantResponse(
creature.type === 'Land',
() => `Expected a Land creature, but got a ${creature.type} creature`,
)
丢入附加选项的响应
import { invariantResponse } from '@epic-web/invariant'
const creature = { name: 'Cerberus', type: 'Underworld' }
invariantResponse(
creature.type === 'Sky',
JSON.stringify({ error: 'Creature must be of type Sky' }),
{ status: 500, headers: { 'Content-Type': 'text/json' } },
)
与 无变化
有三种主要差别. 与 @epic-web/invariant数字 :
- Dev 和 prod 中错误消息相同
- 这很安全
- 我们也支持(反正雷米克) 提出回应的普通案例
与
invariantResponse。 。 。 .
许可证
麻省理工学院