#How do I handle each parameter type possibility differently?

1 messages · Page 1 of 1 (latest)

sage elbowBOT
#
bawdyinkslinger#0

Preview:```ts
export type ElementOf<T> = T extends (infer E)[]
? E
: never
export type Extend<T, E> = T extends E ? E : never
export type EnsureString<T> = T extends string
? string
: never

type TestControllerPromise<T> = any

class Foo<A> {
contains<R>(
expected:
| EnsureString<A>
| ElementOf<A>
| Extend<A, R
...```

spare sphinx
#

I want to know how I can handle this expected parameter differently depending on its type

#

I would find this to be straightforward if the types had some concrete aspect to them, but to me they are pretty abstract

#

I suppose EnsureString might return true if I did typeof expected === "string"? I don't know what to do with the rest.

#

Maybe I can check if ElementOf is an array?

#

So I suppose that would leave Extend as the "else". Is that how I'm supposed to do this?

ancient cedar
#

There's no way to switch on the type, because types don't exist at runtime.

spare sphinx
ancient cedar
#

You'd just write code as if types don't exist