-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ts-type-util-is-readonly-array): create a new package (#28)
* 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
1 parent
510a514
commit dc41544
Showing
12 changed files
with
264 additions
and
12 deletions.
There are no files selected for viewing
12 changes: 1 addition & 11 deletions
12
packages/ts-type-utils/has-own-property/tsconfig.build.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": "../../../tsconfig.base.json" | ||
"extends": "../tsconfig.test.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
127
packages/ts-type-utils/is-readonly-array/index.test-d.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export type isReadonlyArray = (arg: unknown) => arg is readonly unknown[]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../tsconfig.build.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../tsconfig.test.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.