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 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
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
12 changes: 12 additions & 0 deletions readme.md
Expand Up @@ -262,6 +262,18 @@ 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 = []; // unknown[]
m10rten marked this conversation as resolved.
Show resolved Hide resolved
```

## Rules we won't add

### `Object.keys`/`Object.entries`
Expand Down
18 changes: 18 additions & 0 deletions src/entrypoints/new-array.d.ts
@@ -0,0 +1,18 @@
interface ArrayConstructor {
new (arrayLength?: number): unknown[];
(arrayLength?: number): unknown[];

// not sure if the following is needed:
// new <T>(arrayLength: number): T[];
// new <T>(...items: T[]): T[];
// <T>(arrayLength: number): T[];
// <T>(...items: T[]): T[];
// readonly prototype: unknown[];

// following only works with `export` or `export declare` prefixed.
// readonly prototype: unknown[];
}

interface Array<T> {
readonly prototype: unknown[];
m10rten marked this conversation as resolved.
Show resolved Hide resolved
}
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
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" />
14 changes: 14 additions & 0 deletions src/tests/new-array.ts
@@ -0,0 +1,14 @@
import { doNotExecute, Equal, Expect } from "./utils";

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

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

doNotExecute(() => {
// appears to be any[] instead of unknown[]
// const alsoUnkownArr = [];
// type tests = [Expect<Equal<typeof alsoUnkownArr, unknown[]>>];
});