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

Make String.startsWith return ${P}${string} #161

Open
wants to merge 1 commit 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-starts-with": {
"types": "./dist/string-starts-with.d.ts",
"import": "./dist/string-starts-with.mjs",
"default": "./dist/string-starts-with.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-starts-with.d.ts" />
9 changes: 9 additions & 0 deletions src/entrypoints/string-starts-with.d.ts
@@ -0,0 +1,9 @@
interface String {
/**
* Returns true if the sequence of elements of searchString converted to a String is the
* same as the corresponding elements of this object (converted to a String) starting at
* position. Otherwise returns false.
*/
startsWith<P extends string>(searchString: P): this is `${P}${string}`;
startsWith<P extends string>(searchString: P, position: 0): this is `${P}${string}`;
}
29 changes: 29 additions & 0 deletions src/tests/string-starts-with.ts
@@ -0,0 +1,29 @@
import { doNotExecute, Equal, Expect, ExpectFalse } from "./utils";

doNotExecute(() => {
const str: string = '#hashtag';

if (str.startsWith('#')) {
type tests = [Expect<Equal<typeof str, `#${string}`>>];
};

if (str.startsWith('#', 0)) {
type tests = [Expect<Equal<typeof str, `#${string}`>>];
};

if (str.startsWith('#', 1)) {
type tests = [
ExpectFalse<Equal<typeof str, `#${string}`>>,
Expect<Equal<typeof str, string>>,
];
};
});

doNotExecute(() => {
const str = '#hashtag' as const;

if (str.startsWith('#')) {
// type does not narrow
type tests = [Expect<Equal<typeof str, '#hashtag'>>];
};
});