TypeScript$Concept-TopAndBottomTypes
1. Top Types
A top type (symbol: ⊤) is a type that describes any possible value allowed by the system. { any possible value }
TypeScript provides two of these types: any and unknown.
1.1 any
any 就像普通的 JavaScript 变量。在迁移的时候常用。
console.log(window, Promise, setTimeout, "foo")
// (method) Console.log(...data: any[]): void
1.2 unknown
和 any 不同的是,unknown 不能随意读取其属性,需要 applying a type guard。
unknown 常用在接受用户的输入。
unknown 也常用在 handling throwables in a catch block:
function doSomethingRisky() {
if (Math.random() > 0.5) return "ok"
else if (Math.random() > 0.5) throw new Error("Bad luck!")
else throw "Really bad luck"
}
try {
doSomethingRisky()
} catch (e: unknown) {
if (e instanceof Error) {
e
// var e: Error
} else if (typeof e === 'string') {
e
// var e: string
} else {
// Last resort
console.error(e)
//var e: unknown
}
}
当 compiler flag useUnknownInCatchVariables 设置为 true 时,上面的 catch 部分可以简写为 catch(e)。
1.3 Almost top type: object
object 表示对象类型。 { all possible values except for primitives }
1.4 Almost top type: {}
{} 表示除了 null 和 undefined 意外的类型。{ all possible values, except for null and undefined }
{} | null | undefined 表示 { all possible values }
通过 &{},可以清除 null 和 undefined 类型(remove nullability from another type):
type NullableStringOrNumber = string | number | null | undefined;
type StringOrNumber = NullableStringOrNumber & {}
2. Bottom type: never
A bottom type (symbol: ⊥) is a type that describes no possible value allowed by the system. {}
应用:ExhaustivenessChecking。在 never 时抛出异常:
class UnreachableError extends Error {
constructor(_nvr: never, message: string) {
super(message)
}
}
// The exhaustive conditional
if (myVehicle instanceof Truck) {
myVehicle.tow() // Truck
} else if (myVehicle instanceof Car) {
myVehicle.drive() // Car
} else {
// NEITHER!
throw new UnreachableError(
myVehicle,
// Argument of type 'Boat' is not assignable to parameter of type 'never'.
`Unexpected vehicle type: ${myVehicle}`
)
}
3. Unit types
Unit types are types that represent a set of exactly one value.
let num: 65 = 65 // represents the set { 65 }
null & undefined
// `void` type is _almost_ a unit type, but it can check against `undefined` as well