Skip to content

Commit 67858b3

Browse files
committed
feat!: improve type safety
The functions now use generics for improved type safety. BREAKING CHANGE: The return types are now more specific. This may break existing code that relies on less strict typing, but should not affect most users. To preserve the prior behavior, you can cast the return value to `string` via a type assertion.
1 parent f766a56 commit 67858b3

File tree

16 files changed

+53
-17
lines changed

16 files changed

+53
-17
lines changed

lib/node_modules/@stdlib/string/base/capitalize/docs/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
// TypeScript Version: 4.1
2020

21+
type Capitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
22+
2123
/**
2224
* Capitalizes the first character in a string.
2325
*
@@ -40,7 +42,7 @@
4042
* var out = capitalize( 'Hidden Treasures' );
4143
* // returns 'Hidden Treasures'
4244
*/
43-
declare function capitalize( str: string ): string;
45+
declare function capitalize<S extends string>( str: S ): Capitalize<S>;
4446

4547

4648
// EXPORTS //

lib/node_modules/@stdlib/string/base/capitalize/docs/types/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import capitalize = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
capitalize( 'abc' ); // $ExpectType string
26+
capitalize( 'abc' ); // $ExpectType "Abc"
27+
capitalize( 'beep boop' ); // $ExpectType "Beep boop"
28+
capitalize( 'a' ); // $ExpectType "A"
29+
capitalize( 'A' ); // $ExpectType "A"
30+
capitalize( 'abc' as string ); // $ExpectType string
2731
}
2832

2933
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/base/lowercase/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* var str = lowercase( 'bEEp' );
2929
* // returns 'beep'
3030
*/
31-
declare function lowercase( str: string ): string;
31+
declare function lowercase<S extends string>( str: S ): Lowercase<S>;
3232

3333

3434
// EXPORTS //

lib/node_modules/@stdlib/string/base/lowercase/docs/types/test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import lowercase = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
lowercase( 'abc' ); // $ExpectType string
26+
lowercase( 'ABC' ); // $ExpectType "abc"
27+
lowercase( 'Beep BOOP' ); // $ExpectType "beep boop"
28+
lowercase( 'abc' as string ); // $ExpectType string
2729
}
2830

2931
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/base/uncapitalize/docs/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
// TypeScript Version: 4.1
2020

21+
type Uncapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
22+
2123
/**
2224
* Uncapitalizes the first character of a string.
2325
*
@@ -40,7 +42,7 @@
4042
* var out = uncapitalize( 'Hidden Treasures' );
4143
* // returns 'hidden Treasures'
4244
*/
43-
declare function uncapitalize( str: string ): string;
45+
declare function uncapitalize<S extends string>( str: S ): Uncapitalize<S>;
4446

4547

4648
// EXPORTS //

lib/node_modules/@stdlib/string/base/uncapitalize/docs/types/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import uncapitalize = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
uncapitalize( 'Last man standing' ); // $ExpectType string
26+
uncapitalize( 'Last man standing' ); // $ExpectType "last man standing"
27+
uncapitalize( 'Hello World!' ); // $ExpectType "hello World!"
28+
uncapitalize( 'BeepBoop' ); // $ExpectType "beepBoop"
29+
uncapitalize( 'A' ); // $ExpectType "a"
30+
uncapitalize( 'foo' as string ); // $ExpectType string
2731
}
2832

2933
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/base/uppercase/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* var str = uppercase( 'bEEp' );
2929
* // returns 'BEEP'
3030
*/
31-
declare function uppercase( str: string ): string;
31+
declare function uppercase<S extends string>( str: S ): Uppercase<S>;
3232

3333

3434
// EXPORTS //

lib/node_modules/@stdlib/string/base/uppercase/docs/types/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import uppercase = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
uppercase( 'Last man standing' ); // $ExpectType string
26+
uppercase( 'Last man standing' ); // $ExpectType "LAST MAN STANDING"
27+
uppercase( 'Hello World!' ); // $ExpectType "HELLO WORLD!"
28+
uppercase( 'beep' ); // $ExpectType "BEEP"
29+
uppercase( 'BOOP' ); // $ExpectType "BOOP"
30+
uppercase( 'foo' as string ); // $ExpectType string
2731
}
2832

2933
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/capitalize/docs/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
// TypeScript Version: 4.1
2020

21+
type Capitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Uppercase<F>}${R}` : S;
22+
2123
/**
2224
* Capitalizes the first character in a string.
2325
*
@@ -40,7 +42,7 @@
4042
* var out = capitalize( 'Hidden Treasures' );
4143
* // returns 'Hidden Treasures'
4244
*/
43-
declare function capitalize( str: string ): string;
45+
declare function capitalize<S extends string>( str: S ): Capitalize<S>;
4446

4547

4648
// EXPORTS //

lib/node_modules/@stdlib/string/capitalize/docs/types/test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@ import capitalize = require( './index' );
2121

2222
// TESTS //
2323

24-
// The function returns a string...
24+
// The function returns a capitalized string...
2525
{
26-
capitalize( 'abc' ); // $ExpectType string
26+
capitalize( 'abc' ); // $ExpectType "Abc"
27+
capitalize( 'beep boop' ); // $ExpectType "Beep boop"
28+
capitalize( 'a' ); // $ExpectType "A"
29+
capitalize( 'A' ); // $ExpectType "A"
30+
capitalize( 'abc' as string ); // $ExpectType string
2731
}
2832

2933
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/lowercase/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* var str = lowercase( 'bEEp' );
2929
* // returns 'beep'
3030
*/
31-
declare function lowercase( str: string ): string;
31+
declare function lowercase<S extends string>( str: S ): Lowercase<S>;
3232

3333

3434
// EXPORTS //

lib/node_modules/@stdlib/string/lowercase/docs/types/test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ import lowercase = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
lowercase( 'abc' ); // $ExpectType string
26+
lowercase( 'ABC' ); // $ExpectType "abc"
27+
lowercase( 'Beep BOOP' ); // $ExpectType "beep boop"
28+
lowercase( 'abc' as string ); // $ExpectType string
2729
}
2830

2931
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/uncapitalize/docs/types/index.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
// TypeScript Version: 4.1
2020

21+
type Uncapitalize<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
22+
2123
/**
2224
* Uncapitalizes the first character of a string.
2325
*
@@ -40,7 +42,7 @@
4042
* var out = uncapitalize( 'Hidden Treasures' );
4143
* // returns 'hidden Treasures'
4244
*/
43-
declare function uncapitalize( str: string ): string;
45+
declare function uncapitalize<S extends string>( str: S ): Uncapitalize<S>;
4446

4547

4648
// EXPORTS //

lib/node_modules/@stdlib/string/uncapitalize/docs/types/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import uncapitalize = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
uncapitalize( 'Last man standing' ); // $ExpectType string
26+
uncapitalize( 'Last man standing' ); // $ExpectType "last man standing"
27+
uncapitalize( 'Hello World!' ); // $ExpectType "hello World!"
28+
uncapitalize( 'BeepBoop' ); // $ExpectType "beepBoop"
29+
uncapitalize( 'A' ); // $ExpectType "a"
30+
uncapitalize( 'foo' as string ); // $ExpectType string
2731
}
2832

2933
// The compiler throws an error if the function is provided a value other than a string...

lib/node_modules/@stdlib/string/uppercase/docs/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* var str = uppercase( 'bEEp' );
2929
* // returns 'BEEP'
3030
*/
31-
declare function uppercase( str: string ): string;
31+
declare function uppercase<S extends string>( str: S ): Uppercase<S>;
3232

3333

3434
// EXPORTS //

lib/node_modules/@stdlib/string/uppercase/docs/types/test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ import uppercase = require( './index' );
2323

2424
// The function returns a string...
2525
{
26-
uppercase( 'Last man standing' ); // $ExpectType string
26+
uppercase( 'Last man standing' ); // $ExpectType "LAST MAN STANDING"
27+
uppercase( 'Hello World!' ); // $ExpectType "HELLO WORLD!"
28+
uppercase( 'beep' ); // $ExpectType "BEEP"
29+
uppercase( 'BOOP' ); // $ExpectType "BOOP"
30+
uppercase( 'foo' as string ); // $ExpectType string
2731
}
2832

2933
// The compiler throws an error if the function is provided a value other than a string...

0 commit comments

Comments
 (0)