ts-join
Global typing utility for ReadonlyArray.join to return the value of it as a type
Without ts-join
:
join
(inArray
andReadonlyArray
) returnsstring
join
(inArray
andReadonlyArray
) doesn't react if asymbol
is contained inside the array the method is being applied to
With ts-join
:
arr.join(s)
returns the expected result of thejoin
operation whenarr
is of typereadonly any[]
ands
is a constantstring
join
(inArray
andReadonlyArray
) returnsnever
if asymbol
is contained inside the array the method is being applied to
Install
npm install --save-dev @zanganken/ts-join
Usage
As a simple helper
Create a ts-join.d.ts
file in your project with these contents:
// Do not add any other lines of code to this file!
import "@zanganken/ts-join";
As a dependency in your project
For these imports to work, you'll need to ensure that, in your
tsconfig.json
,moduleResolution
is set toNodeNext
,Node16
orBundler
.
Import arrayJoin()
in your project and use it like this:
import { arrayJoin } from "@zanganken/ts-join/array-join";
// returns "a/b/c" as a type
const something = arrayJoin(<const>["a", "b", "c"], "/")
or
Import ArrayJoin<T, S>
to use it as a type in your projects:
import { type ArrayJoin } from "@zanganken/ts-join/array-join";
// returns "a/b/c" as a type
type Something = ArrayJoin<readonly ["a", "b", "c"], "/">