typescript 类型判断与函数参数处理
在 typescript 开发中,我们经常需要根据参数的类型进行不同的处理。本文将探讨如何在 typescript 函数体中有效地判断 interface 类型参数,并提供几种解决方案。
假设我们定义了两个 interface:
interface person { name: string; age: number; } interface animal { food: string; kind: string; }
现在有一个函数需要处理这两种类型的参数:
function test(some: person | animal) { // todo: 如何判断 some 的具体类型并分别处理? }
直接使用 javascript 中的 typeof 或 instanceof 并不能完全满足 typescript 的类型安全需求。 虽然可以使用 'name' in some 或 'food' in some 进行属性检查,但这本质上是在运行时进行类型判断,而非 typescript 的编译时类型检查。 那么,如何在 typescript 层面实现精确的类型判断呢?
一种方法是编写谓词函数 (predicate) 来手动检查对象的属性。谓词函数的返回值类型为 value is sometype,这能让 typescript 编译器在后续代码中自动进行类型收窄。
function isperson(o: unknown): o is person { const obj = o as partial<person> | null | undefined; return typeof obj?.name === 'string' && typeof obj?.age === 'number'; } function isanimal(o: unknown): o is animal { const obj = o as partial<animal> | null | undefined; return typeof obj?.food === 'string' && typeof obj?.kind === 'string'; } function test(some: person | animal) { if (isperson(some)) { // 现在 some 的类型被收窄为 person console.log(some.name); } if (isanimal(some)) { // 现在 some 的类型被收窄为 animal console.log(some.food); } }
这种方法虽然有效,但对于复杂的 interface,编写谓词函数会变得冗长且容易出错。 因此,可以使用类型检查库,例如 io-ts,来简化这个过程。 io-ts 提供了更简洁的类型定义和验证方式。
另一种方法是使用类 (class) 来定义对象,并利用 instanceof 进行类型检查。
class Person { name: string; age: number; constructor(init: person) { Object.assign(this, init); } } class Animal { food: string; kind: string; constructor(init: animal) { Object.assign(this, init); } } function test(some: Person | Animal) { if (some instanceof Person) { console.log(some.name); } if (some instanceof Animal) { console.log(some.food); } } test(new Person({ name: 'tom', age: 3 })); test(new Animal({ kind: 'cat', food: 'fish' }));
这种面向对象的方式,可以清晰地定义对象的类型,并通过 instanceof 进行准确的类型判断。 但是,这需要将代码改写成面向对象风格。