Skip to content

Commit

Permalink
feat(ts-type-util-is-readonly-array): create a new package (#28)
Browse files Browse the repository at this point in the history
* chore(ts-type-util-is-readonly-array): create a directory for the `@sounisi5011/ts-type-util-is-readonly-array` package

* chore(./packages/ts-type-utils/): move tsconfig file with same settings into parent directory

* feat(ts-type-util-is-readonly-array): create a new package

* test(ts-type-util-is-readonly-array): add tests for using tsd

* test(ts-type-util-is-readonly-array): fix type testing

* test(ts-type-util-is-readonly-array): refactor the tests of type

* test(ts-type-util-is-readonly-array): add tests of type

* docs(ts-type-util-is-readonly-array): add `README.md`

* docs(ts-type-util-is-readonly-array): fix the `description` field in the `package.json` file
  • Loading branch information
sounisi5011 authored Apr 10, 2021
1 parent 510a514 commit dc41544
Show file tree
Hide file tree
Showing 12 changed files with 264 additions and 12 deletions.
12 changes: 1 addition & 11 deletions packages/ts-type-utils/has-own-property/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"declarationMap": false,

/* Advanced Options */
"emitDeclarationOnly": true
},
"exclude": ["*.test-d.ts"]
"extends": "../tsconfig.build.json"
}
2 changes: 1 addition & 1 deletion packages/ts-type-utils/has-own-property/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "../../../tsconfig.base.json"
"extends": "../tsconfig.test.json"
}
57 changes: 57 additions & 0 deletions packages/ts-type-utils/is-readonly-array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# @sounisi5011/ts-type-util-is-readonly-array

[![Go to the latest release page on npm](https://img.shields.io/npm/v/@sounisi5011/ts-type-util-is-readonly-array.svg)](https://www.npmjs.com/package/@sounisi5011/ts-type-util-is-readonly-array)

Fix the type definition of [`Array.isArray()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) method to accept readonly arrays.

## Installation

```sh
npm install @sounisi5011/ts-type-util-is-readonly-array
```

```sh
yarn add @sounisi5011/ts-type-util-is-readonly-array
```

```sh
pnpm add @sounisi5011/ts-type-util-is-readonly-array
```

## Usage

```ts
import { isReadonlyArray } from '@sounisi5011/ts-type-util-is-readonly-array';

const isArray = Array.isArray as isReadonlyArray;

if (isArray(value)) {
// ...
}

function fn(param: string | readonly string[]) {
if (isArray(param)) {
// ...
} else {
// ...
}
}
```

or

```ts
import { isReadonlyArray } from '@sounisi5011/ts-type-util-is-readonly-array';

if ((Array.isArray as isReadonlyArray)(value)) {
// ...
}

function fn(param: string | readonly string[]) {
if ((Array.isArray as isReadonlyArray)(param)) {
// ...
} else {
// ...
}
}
```
127 changes: 127 additions & 0 deletions packages/ts-type-utils/is-readonly-array/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import { expectType } from 'tsd';

import type { isReadonlyArray } from '.';

const isArray = Array.isArray as isReadonlyArray;

// ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- //

export function test1(param: string | readonly string[]): void {
if (isArray(param)) {
expectType<readonly string[]>(param);
} else {
expectType<string>(param);
}

if ((Array.isArray as isReadonlyArray)(param)) {
expectType<readonly string[]>(param);
} else {
expectType<string>(param);
}
}

export function test2(param: string | string[]): void {
if (isArray(param)) {
expectType<string[]>(param);
} else {
expectType<string>(param);
}

if ((Array.isArray as isReadonlyArray)(param)) {
expectType<string[]>(param);
} else {
expectType<string>(param);
}
}

export function test3(param: boolean | string[] | readonly number[]): void {
if (isArray(param)) {
expectType<string[] | readonly number[]>(param);
} else {
expectType<boolean>(param);
}

if ((Array.isArray as isReadonlyArray)(param)) {
expectType<string[] | readonly number[]>(param);
} else {
expectType<boolean>(param);
}
}

export function test4(param: unknown): void {
if (isArray(param)) {
expectType<readonly unknown[]>(param);
} else {
expectType<unknown>(param);
}
}

export function test5<T>(param: T): void {
if (isArray(param)) {
expectType<T & readonly unknown[]>(param);
} else {
expectType<T>(param);
}
}

/* eslint-disable @typescript-eslint/ban-types */
export function test6(param: {}): void {
if (isArray(param)) {
expectType<readonly unknown[]>(param);
} else {
expectType<{}>(param);
}
/* eslint-enable */
}

/*
* see https://github.com/orta/TypeScript/blob/c69b255bf69469838a3b755961471a2cdd63fea7/tests/cases/compiler/consistentUnionSubtypeReduction.ts
*/

declare const a: readonly string[] | string;
declare const b: string[] | string;
declare const c: unknown;

if (isArray(a)) {
expectType<readonly string[]>(a);
} else {
expectType<string>(a);
}
expectType<readonly string[] | string>(a);

if (isArray(b)) {
expectType<string[]>(b);
} else {
expectType<string>(b);
}
expectType<string[] | string>(b);

if (isArray(c)) {
expectType<readonly unknown[]>(c);
}

export function f<T>(_x: T): void {
/* eslint-disable @typescript-eslint/no-explicit-any */
const a: readonly T[] | string = null as any;
const b: T[] | string = null as any;
const c: T = null as any;
/* eslint-enable */

if (isArray(a)) {
expectType<readonly T[]>(a);
} else {
expectType<string>(a);
}
expectType<readonly T[] | string>(a);

if (isArray(b)) {
expectType<T[]>(b);
} else {
expectType<string>(b);
}
expectType<T[] | string>(b);

if (isArray(c)) {
expectType<T & readonly unknown[]>(c);
}
}
1 change: 1 addition & 0 deletions packages/ts-type-utils/is-readonly-array/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type isReadonlyArray = (arg: unknown) => arg is readonly unknown[];
41 changes: 41 additions & 0 deletions packages/ts-type-utils/is-readonly-array/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "@sounisi5011/ts-type-util-is-readonly-array",
"version": "0.0.0",
"description": "Fix the type definition of `Array.isArray()` method to accept readonly arrays",
"keywords": [
"ReadonlyArray",
"array",
"isArray",
"isReadonlyArray",
"readonly",
"ts",
"types",
"typescript",
"util",
"utility"
],
"homepage": "https://github.com/sounisi5011/npm-packages/tree/main/packages/ts-type-utils/is-readonly-array#readme",
"bugs": {
"url": "https://github.com/sounisi5011/npm-packages/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/sounisi5011/npm-packages.git",
"directory": "packages/ts-type-utils/is-readonly-array"
},
"license": "MIT",
"author": "sounisi5011",
"types": "./index.d.ts",
"files": [
"index.d.ts"
],
"scripts": {
"build": "tsc -p ./tsconfig.build.json",
"lint:tsc": "tsc --noEmit",
"test": "run-s build test:tsd",
"test:tsd": "tsd"
},
"devDependencies": {
"tsd": "0.14.0"
}
}
3 changes: 3 additions & 0 deletions packages/ts-type-utils/is-readonly-array/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.build.json"
}
3 changes: 3 additions & 0 deletions packages/ts-type-utils/is-readonly-array/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../tsconfig.test.json"
}
3 changes: 3 additions & 0 deletions packages/ts-type-utils/tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.base.json"
}
13 changes: 13 additions & 0 deletions packages/ts-type-utils/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"declarationMap": false,

/* Advanced Options */
"emitDeclarationOnly": true
},
"exclude": ["*/*.test-d.ts"]
}
9 changes: 9 additions & 0 deletions packages/ts-type-utils/tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */

/* Basic Options */
"noEmit": true
}
}
5 changes: 5 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc41544

Please sign in to comment.