Skip to content
Closed
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
2 changes: 2 additions & 0 deletions src/lib/es2015.core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ interface String {
* same as the corresponding elements of this object (converted to a String) starting at
* endPosition – length(this). Otherwise returns false.
*/
endsWith<T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`;
endsWith(searchString: string, endPosition?: number): boolean;

/**
Expand Down Expand Up @@ -431,6 +432,7 @@ interface String {
* same as the corresponding elements of this object (converted to a String) starting at
* position. Otherwise returns false.
*/
startsWith<T extends string>(searchString: T, position?: 0): this is `${T}${string}`;
startsWith(searchString: string, position?: number): boolean;

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/baselines/reference/stringEndsWith.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
tests/cases/compiler/stringEndsWith.ts(9,46): error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.
tests/cases/compiler/stringEndsWith.ts(10,46): error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.
tests/cases/compiler/stringEndsWith.ts(11,56): error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.
tests/cases/compiler/stringEndsWith.ts(12,56): error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.


==== tests/cases/compiler/stringEndsWith.ts (4 errors) ====
declare var test: string;
declare function requirePostfix(input: `${string}test`): void;

// works
if (test.endsWith('test')) requirePostfix(test);
if (test.endsWith('test', undefined)) requirePostfix(test);

// errors
if (test.endsWith('test', 0)) requirePostfix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.
if (test.endsWith('test', 1)) requirePostfix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.
if (test.endsWith('test', 0 as number)) requirePostfix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.
if (test.endsWith('test', 1 as number)) requirePostfix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`${string}test`'.

30 changes: 30 additions & 0 deletions tests/baselines/reference/stringEndsWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [stringEndsWith.ts]
declare var test: string;
declare function requirePostfix(input: `${string}test`): void;

// works
if (test.endsWith('test')) requirePostfix(test);
if (test.endsWith('test', undefined)) requirePostfix(test);

// errors
if (test.endsWith('test', 0)) requirePostfix(test);
if (test.endsWith('test', 1)) requirePostfix(test);
if (test.endsWith('test', 0 as number)) requirePostfix(test);
if (test.endsWith('test', 1 as number)) requirePostfix(test);


//// [stringEndsWith.js]
// works
if (test.endsWith('test'))
requirePostfix(test);
if (test.endsWith('test', undefined))
requirePostfix(test);
// errors
if (test.endsWith('test', 0))
requirePostfix(test);
if (test.endsWith('test', 1))
requirePostfix(test);
if (test.endsWith('test', 0))
requirePostfix(test);
if (test.endsWith('test', 1))
requirePostfix(test);
53 changes: 53 additions & 0 deletions tests/baselines/reference/stringEndsWith.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/stringEndsWith.ts ===
declare var test: string;
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

declare function requirePostfix(input: `${string}test`): void;
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>input : Symbol(input, Decl(stringEndsWith.ts, 1, 32))

// works
if (test.endsWith('test')) requirePostfix(test);
>test.endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))
>endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

if (test.endsWith('test', undefined)) requirePostfix(test);
>test.endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))
>endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>undefined : Symbol(undefined)
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

// errors
if (test.endsWith('test', 0)) requirePostfix(test);
>test.endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))
>endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

if (test.endsWith('test', 1)) requirePostfix(test);
>test.endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))
>endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

if (test.endsWith('test', 0 as number)) requirePostfix(test);
>test.endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))
>endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

if (test.endsWith('test', 1 as number)) requirePostfix(test);
>test.endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))
>endsWith : Symbol(String.endsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePostfix : Symbol(requirePostfix, Decl(stringEndsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringEndsWith.ts, 0, 11))

77 changes: 77 additions & 0 deletions tests/baselines/reference/stringEndsWith.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
=== tests/cases/compiler/stringEndsWith.ts ===
declare var test: string;
>test : string

declare function requirePostfix(input: `${string}test`): void;
>requirePostfix : (input: `${string}test`) => void
>input : `${string}test`

// works
if (test.endsWith('test')) requirePostfix(test);
>test.endsWith('test') : boolean
>test.endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>test : string
>endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>'test' : "test"
>requirePostfix(test) : void
>requirePostfix : (input: `${string}test`) => void
>test : `${string}test`

if (test.endsWith('test', undefined)) requirePostfix(test);
>test.endsWith('test', undefined) : boolean
>test.endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>test : string
>endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>'test' : "test"
>undefined : undefined
>requirePostfix(test) : void
>requirePostfix : (input: `${string}test`) => void
>test : `${string}test`

// errors
if (test.endsWith('test', 0)) requirePostfix(test);
>test.endsWith('test', 0) : boolean
>test.endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>test : string
>endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>'test' : "test"
>0 : 0
>requirePostfix(test) : void
>requirePostfix : (input: `${string}test`) => void
>test : string

if (test.endsWith('test', 1)) requirePostfix(test);
>test.endsWith('test', 1) : boolean
>test.endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>test : string
>endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>'test' : "test"
>1 : 1
>requirePostfix(test) : void
>requirePostfix : (input: `${string}test`) => void
>test : string

if (test.endsWith('test', 0 as number)) requirePostfix(test);
>test.endsWith('test', 0 as number) : boolean
>test.endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>test : string
>endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>'test' : "test"
>0 as number : number
>0 : 0
>requirePostfix(test) : void
>requirePostfix : (input: `${string}test`) => void
>test : string

if (test.endsWith('test', 1 as number)) requirePostfix(test);
>test.endsWith('test', 1 as number) : boolean
>test.endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>test : string
>endsWith : { <T extends string>(searchString: T, endPosition?: undefined): this is `${string}${T}`; (searchString: string, endPosition?: number): boolean; }
>'test' : "test"
>1 as number : number
>1 : 1
>requirePostfix(test) : void
>requirePostfix : (input: `${string}test`) => void
>test : string

25 changes: 25 additions & 0 deletions tests/baselines/reference/stringStartsWith.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
tests/cases/compiler/stringStartsWith.ts(10,47): error TS2345: Argument of type 'string' is not assignable to parameter of type '`test${string}`'.
tests/cases/compiler/stringStartsWith.ts(11,57): error TS2345: Argument of type 'string' is not assignable to parameter of type '`test${string}`'.
tests/cases/compiler/stringStartsWith.ts(12,57): error TS2345: Argument of type 'string' is not assignable to parameter of type '`test${string}`'.


==== tests/cases/compiler/stringStartsWith.ts (3 errors) ====
declare var test: string;
declare function requirePrefix(input: `test${string}`): void;

// works
if (test.startsWith('test')) requirePrefix(test);
if (test.startsWith('test', undefined)) requirePrefix(test);
if (test.startsWith('test', 0)) requirePrefix(test);

// errors
if (test.startsWith('test', 1)) requirePrefix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`test${string}`'.
if (test.startsWith('test', 0 as number)) requirePrefix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`test${string}`'.
if (test.startsWith('test', 1 as number)) requirePrefix(test);
~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type '`test${string}`'.

30 changes: 30 additions & 0 deletions tests/baselines/reference/stringStartsWith.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [stringStartsWith.ts]
declare var test: string;
declare function requirePrefix(input: `test${string}`): void;

// works
if (test.startsWith('test')) requirePrefix(test);
if (test.startsWith('test', undefined)) requirePrefix(test);
if (test.startsWith('test', 0)) requirePrefix(test);

// errors
if (test.startsWith('test', 1)) requirePrefix(test);
if (test.startsWith('test', 0 as number)) requirePrefix(test);
if (test.startsWith('test', 1 as number)) requirePrefix(test);


//// [stringStartsWith.js]
// works
if (test.startsWith('test'))
requirePrefix(test);
if (test.startsWith('test', undefined))
requirePrefix(test);
if (test.startsWith('test', 0))
requirePrefix(test);
// errors
if (test.startsWith('test', 1))
requirePrefix(test);
if (test.startsWith('test', 0))
requirePrefix(test);
if (test.startsWith('test', 1))
requirePrefix(test);
53 changes: 53 additions & 0 deletions tests/baselines/reference/stringStartsWith.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
=== tests/cases/compiler/stringStartsWith.ts ===
declare var test: string;
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

declare function requirePrefix(input: `test${string}`): void;
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>input : Symbol(input, Decl(stringStartsWith.ts, 1, 31))

// works
if (test.startsWith('test')) requirePrefix(test);
>test.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

if (test.startsWith('test', undefined)) requirePrefix(test);
>test.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>undefined : Symbol(undefined)
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

if (test.startsWith('test', 0)) requirePrefix(test);
>test.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

// errors
if (test.startsWith('test', 1)) requirePrefix(test);
>test.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

if (test.startsWith('test', 0 as number)) requirePrefix(test);
>test.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

if (test.startsWith('test', 1 as number)) requirePrefix(test);
>test.startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))
>startsWith : Symbol(String.startsWith, Decl(lib.es2015.core.d.ts, --, --), Decl(lib.es2015.core.d.ts, --, --))
>requirePrefix : Symbol(requirePrefix, Decl(stringStartsWith.ts, 0, 25))
>test : Symbol(test, Decl(stringStartsWith.ts, 0, 11))

77 changes: 77 additions & 0 deletions tests/baselines/reference/stringStartsWith.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
=== tests/cases/compiler/stringStartsWith.ts ===
declare var test: string;
>test : string

declare function requirePrefix(input: `test${string}`): void;
>requirePrefix : (input: `test${string}`) => void
>input : `test${string}`

// works
if (test.startsWith('test')) requirePrefix(test);
>test.startsWith('test') : boolean
>test.startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>test : string
>startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>'test' : "test"
>requirePrefix(test) : void
>requirePrefix : (input: `test${string}`) => void
>test : `test${string}`

if (test.startsWith('test', undefined)) requirePrefix(test);
>test.startsWith('test', undefined) : boolean
>test.startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>test : string
>startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>'test' : "test"
>undefined : undefined
>requirePrefix(test) : void
>requirePrefix : (input: `test${string}`) => void
>test : `test${string}`

if (test.startsWith('test', 0)) requirePrefix(test);
>test.startsWith('test', 0) : boolean
>test.startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>test : string
>startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>'test' : "test"
>0 : 0
>requirePrefix(test) : void
>requirePrefix : (input: `test${string}`) => void
>test : `test${string}`

// errors
if (test.startsWith('test', 1)) requirePrefix(test);
>test.startsWith('test', 1) : boolean
>test.startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>test : string
>startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>'test' : "test"
>1 : 1
>requirePrefix(test) : void
>requirePrefix : (input: `test${string}`) => void
>test : string

if (test.startsWith('test', 0 as number)) requirePrefix(test);
>test.startsWith('test', 0 as number) : boolean
>test.startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>test : string
>startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>'test' : "test"
>0 as number : number
>0 : 0
>requirePrefix(test) : void
>requirePrefix : (input: `test${string}`) => void
>test : string

if (test.startsWith('test', 1 as number)) requirePrefix(test);
>test.startsWith('test', 1 as number) : boolean
>test.startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>test : string
>startsWith : { <T extends string>(searchString: T, position?: 0): this is `${T}${string}`; (searchString: string, position?: number): boolean; }
>'test' : "test"
>1 as number : number
>1 : 1
>requirePrefix(test) : void
>requirePrefix : (input: `test${string}`) => void
>test : string

Loading