确定在打字稿中可能具有多种类型的参数类型
determine type of parameter that could have multiple types in typescript
给定一个具有不同类型参数的函数,我如何找出传递给该函数的类型?
示例
interface SomeCustomInterface {
title: string
}
interface OtherCustomInterface {
subtitle: string
}
interface A {
value: string
}
interface B {
value: number
}
interface C {
value: SomeCustomInterface
}
interface D {
value: OtherCustomInterface
}
function doSomething(parameter: A | B | C | D): string {
switch(parameter.type) {
// I know the cases are not valid TS. What should I do instead?
case A:
return parameter.value
case B:
return parameter.value.toString()
case C:
return parameter.value.title
case D:
return parameter.value.subtitle
}
}
我知道有类型保护,但我对那些有顾虑
他们需要能够唯一地识别每种类型。我看到有些人添加了 属性 kind
或 type
以允许他们识别类型以便对其进行类型保护。不过,这对我来说似乎是很多开销和样板文件。
您需要为每种类型编写一个自定义函数,例如 type is A
或 type is B
,这在我的上下文中会再次导致大量开销。
在打字稿中处理这个问题的合适方法是什么?
基本上只有 接受的答案中描述的 2 个选项。
What you can do is check that the shape of an object is what you expect, and TypeScript can assert the type at compile time using a user-defined type guard that returns true (annotated return type is a "type predicate" of the form arg is T) if the shape matches your expectation:
For class types you can use JavaScript's instanceof
to determine the class an instance comes from, and TypeScript will narrow the type in the type-checker automatically.
我自己的旁注:
如果您有相同的属性名,您可以使用泛型重构您的代码,例如:
interface A {
value: string
}
interface B {
value: number
}
interface C {
value: SomeCustomInterface
}
interface D {
value: OtherCustomInterface
}
可以
interface GenericInterface<T>{
value: T
}
给定一个具有不同类型参数的函数,我如何找出传递给该函数的类型?
示例
interface SomeCustomInterface {
title: string
}
interface OtherCustomInterface {
subtitle: string
}
interface A {
value: string
}
interface B {
value: number
}
interface C {
value: SomeCustomInterface
}
interface D {
value: OtherCustomInterface
}
function doSomething(parameter: A | B | C | D): string {
switch(parameter.type) {
// I know the cases are not valid TS. What should I do instead?
case A:
return parameter.value
case B:
return parameter.value.toString()
case C:
return parameter.value.title
case D:
return parameter.value.subtitle
}
}
我知道有类型保护,但我对那些有顾虑
他们需要能够唯一地识别每种类型。我看到有些人添加了 属性
kind
或type
以允许他们识别类型以便对其进行类型保护。不过,这对我来说似乎是很多开销和样板文件。您需要为每种类型编写一个自定义函数,例如
type is A
或type is B
,这在我的上下文中会再次导致大量开销。
在打字稿中处理这个问题的合适方法是什么?
基本上只有
What you can do is check that the shape of an object is what you expect, and TypeScript can assert the type at compile time using a user-defined type guard that returns true (annotated return type is a "type predicate" of the form arg is T) if the shape matches your expectation:
For class types you can use JavaScript's
instanceof
to determine the class an instance comes from, and TypeScript will narrow the type in the type-checker automatically.
我自己的旁注:
如果您有相同的属性名,您可以使用泛型重构您的代码,例如:
interface A {
value: string
}
interface B {
value: number
}
interface C {
value: SomeCustomInterface
}
interface D {
value: OtherCustomInterface
}
可以
interface GenericInterface<T>{
value: T
}