-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make String.startsWith return
${P}${string}
- Loading branch information
1 parent
4d44ea0
commit 2d790a3
Showing
4 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
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
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
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 @@ | ||
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}`; | ||
} |
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,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'>>]; | ||
}; | ||
}); |