1

What is the TypeScript equivalent of this?

sessions: PropTypes.oneOfType([
    PropTypes.array,
    PropTypes.object,
]).isRequired,

I translated this to the following 'point':

export type Sessions = [Object | Array];

However, I'm not sure how I should replace PropTypes.array and PropTypes.object.

2 Answers 2

1

Like any other type

static propTypes = {
    sessions: Array | Object
}

If session is an array of either type (but it's not what is stated in the PropTypes expression):

sessions: (Array | Object)[]

If it's not required:

sessions?: Array | Object

typeless Array can also be expressed as [].

(Object | Array)[] can also be expressed as Array<Object | Array>.

In some case Object can also be expressed as {} and this way can declare props: {a: number, b: string}

As explained here: https://www.typescriptlang.org/docs/handbook/advanced-types.html

Sign up to request clarification or add additional context in comments.

Comments

0

You can consider this

type TypeSessions = Object | Array;
const sessions: TypeSessions = [];

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.