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

improve types for String.split #110

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions package.json
Expand Up @@ -72,6 +72,11 @@
"types": "./dist/array-index-of.d.ts",
"import": "./dist/array-index-of.mjs",
"default": "./dist/array-index-of.js"
},
"./string-split": {
"types": "./dist/string-split.d.ts",
"import": "./dist/string-split.mjs",
"default": "./dist/string-split.js"
}
},
"keywords": [],
Expand Down
1 change: 1 addition & 0 deletions src/entrypoints/recommended.d.ts
Expand Up @@ -6,3 +6,4 @@
/// <reference path="set-has.d.ts" />
/// <reference path="map-has.d.ts" />
/// <reference path="array-index-of.d.ts" />
/// <reference path="string-split.d.ts" />
5 changes: 5 additions & 0 deletions src/entrypoints/string-split.d.ts
@@ -0,0 +1,5 @@
interface String {
split<Separator extends string, Limit extends number>(separator: Separator, limit?: Limit): Limit extends 0 ? []
: Separator extends '' ? string[]
Copy link

@lo1tuma lo1tuma Sep 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be dangerous when the type is not inferred by a literal and an empty string is passed as type string. For example:

const foo: string = '';
''.split(foo); // <-- would incorrectly yield [string, ...string[]]

: [string, ...string[]]
}
43 changes: 43 additions & 0 deletions src/tests/string-split.ts
@@ -0,0 +1,43 @@

import { doNotExecute, Equal, Expect } from "./utils";

doNotExecute(() => {
const result = ''.split('')
type tests = [Expect<Equal<typeof result, string[]>>];
});

doNotExecute(() => {
const result = ''.split('')[0]

type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const result = 'value'.split('test')

type tests = [Expect<Equal<typeof result, [string, ...string[]]>>];
});

doNotExecute(() => {
const result = 'value'.split('test')[0]

type tests = [Expect<Equal<typeof result, string>>];
});

doNotExecute(() => {
const result = 'value'.split('test')[1]

type tests = [Expect<Equal<typeof result, string | undefined>>];
});

doNotExecute(() => {
const result = 'value'.split('test', 0)

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

doNotExecute(() => {
const result = 'value'.split('test', 1)

type tests = [Expect<Equal<typeof result, [string, ...string[]]>>];
});
1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -6,6 +6,7 @@
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"noUncheckedIndexedAccess": true, /* Add undefined to a type when accessed using an index. */
},
"exclude": [
"dist",
Expand Down