Reputation: 219
I have a map function and my function's parameter's type is an array of strings or number. I'm defining it like:
(param: (string | number)[]) => ...
however I want to beauty it like:
(param: StringOrNumber)[]) => ...
since I'm using that param in many functions and found that pipeline in middle decreases readibility. Is there a way to do that by default in Typescript or am I need to define a custom type for that?
Upvotes: 2
Views: 1947
Reputation: 46
You would need to define a custom type for this, like type StringOrNumber = string | number
. However, if you include a custom type such as this in a declaration file (eg. in a file named types.d.ts
, where the .d.ts
suffix indicates to the TypeScript compiler that it is a declaration file) somewhere in your project, then that type will be globally available in the other files in your project.
I would recommend reading up on declaration files in the documentation here, though it's not super clear. https://www.typescriptlang.org/v2/docs/handbook/declaration-files/introduction.html
Upvotes: 2
Reputation: 13539
you need to define a new type and use it everywhere, that's totally fine, or you could use Array<string | number>
, what is more readable IMO.
Upvotes: 0