Item 36: Use a Distinct Type for Special Values
要点
- Avoid special values that are assignable to regular values in a type. They will reduce TypeScript's ability to find bugs in your code.
- Prefer
null
orundefined
as a special value instead of0
,-1
, or""
. - Consider using a tagged union rather than
null
orundefined
if the meaning of those values isn't clear. - 避免将特殊值赋给常规类型的值,这会降低 TypeScript 发现代码中 bug 的能力。
- 优先使用
null
或undefined
作为特殊值,而不是0
、-1
或""
。 - 如果特殊值的意义不明确,考虑使用标签联合类型(tagged union)代替
null
或undefined
。
正文
ts
function splitAround<T>(vals: readonly T[], val: T): [T[], T[]] {
const index = vals.indexOf(val)
return [vals.slice(0, index), vals.slice(index + 1)]
}
ts
function safeIndexOf<T>(vals: readonly T[], val: T): number | null {
const index = vals.indexOf(val)
return index === -1 ? null : index
}
ts
function splitAround<T>(vals: readonly T[], val: T): [T[], T[]] {
const index = safeIndexOf(vals, val)
return [vals.slice(0, index), vals.slice(index + 1)]
// ~~~~~ ~~~~~ 'index' is possibly 'null'
}
ts
function splitAround<T>(vals: readonly T[], val: T): [T[], T[]] {
const index = safeIndexOf(vals, val)
if (index === null) {
return [[...vals], []]
}
return [vals.slice(0, index), vals.slice(index + 1)] // ok
}
ts
interface Product {
title: string
priceDollars: number
}
ts
interface Product {
title: string
/** Price of the product in dollars, or -1 if price is unknown */
priceDollars: number
}
ts
// @strictNullChecks: false
const truck: Product = {
title: 'Tesla Cybertruck',
priceDollars: null, // ok
}