0

In Typescript, if I'm defining a type that stipulates only strings can go into an array, I'm able to define that like this:

type stringArray = string[]

I'm able to specify that certain kinds of objects may go into an array like this:

type myObject  = { 
    one: string,
    two: string,
}
type objectArray = myObject[]

However, I'm able to accomplish something very similar with generics, like this:

type stringArray = Array<string>

and

type objectArray = Array<myObject>

Is there any advantage to using either approach? When should one use the former and when is the latter a better approach?

1
  • 2
    iirc, the bracket syntax is just some sugar, since it looks cleaner Commented Aug 7, 2020 at 22:00

1 Answer 1

2

Array<T> and T[] are 100% equivalent.

Which to use is mostly a code style choice. Though the T[] syntax is usually preferred in most codebases that I've seen as it looks like an array, and at a glance it is shorter.

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.