3

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?

1
  • function mockingcase(input: string|string[], options?: { random?: boolean }): string Commented Feb 27, 2019 at 9:45

1 Answer 1

2

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.