Alo
Alo

Reputation: 31

Function declaration with multiple type

I have the following function that takes a String and return a String such as

mockingcase('foobar')
// => fOoBaR

The project has a Typescript declaration file. I don't know much about Typescript (read nothing other than the last hour spent reading the doc.)

The function mockingcase has now the ability to return a String from an Array of string

mockingcase(['foo','bar'])
// => 'fOoBaR'

How do I change the typescript declaration file so it can take a String or an Array?

original:

function mockingcase(input: string, options?: { random?: boolean }): string;

my idea:

function mockingcase(input: string|array, options?: { random?: boolean }): string;

Am I completely wrong?

Upvotes: 3

Views: 104

Answers (1)

Joe Clay
Joe Clay

Reputation: 35797

You're close - array types must also define what the type of the object contained within the array is:

input: string | string[]

Or:

input: string | Array<string>

Upvotes: 2

Related Questions