Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Started new-array unknown type #66

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
"types": "./dist/utils.d.ts",
"import": "./dist/utils.mjs",
"default": "./dist/utils.js"
},
"./new-array": {
"types": "./dist/new-array.d.ts",
"import": "./dist/new-array.mjs",
"default": "./dist/new-array.js"
}
},
"keywords": [],
Expand Down
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ const validate = (input: unknown) => {
};
```

### Make `new Array()` return `unknown[]`

With `new Array()` you can introduce `any`'s into your code:

```ts
import "@total-typescript/ts-reset/new-array";

const arr = new Array(); // unknown[]

const arr2 = new Array(10); // length is 10, but the type is unknown[]

const inferred = new Array(1, 2, 3); // number[]
```

## Rules we won't add

### `Object.keys`/`Object.entries`
Expand Down
4 changes: 4 additions & 0 deletions src/entrypoints/new-array.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface ArrayConstructor {
new (arrayLength?: number): unknown[];
(arrayLength?: number): unknown[];
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/// <reference path="json-parse.d.ts" />
/// <reference path="array-includes.d.ts" />
/// <reference path="set-has.d.ts" />
/// <reference path="new-array.d.ts" />
22 changes: 22 additions & 0 deletions src/tests/new-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(() => {
// check that new array is inferred correctly to unknown[]
const unknownArr = new Array();

type tests = [Expect<Equal<typeof unknownArr, unknown[]>>];
});
m10rten marked this conversation as resolved.
Show resolved Hide resolved

doNotExecute(() => {
// array with length property
const unknownArr = new Array(10);

type tests = [Expect<Equal<typeof unknownArr, unknown[]>>];
});

doNotExecute(() => {
// array with items
const numberArray = new Array(1, 2);

type tests = [Expect<Equal<typeof numberArray, number[]>>];
});