Skip to content

Commit 48981c2

Browse files
authored
🤖 Merge PR #70966 [lodash] enhance _.capitalize to return a capitalized string type by @Ha-limLee
1 parent 62292df commit 48981c2

File tree

4 files changed

+26
-9
lines changed

4 files changed

+26
-9
lines changed

‎types/lodash/common/lang.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _ = require("../index");
2-
// eslint-disable-next-line @definitelytyped/strict-export-declare-modifiers
2+
// eslint-disable-next-line @definitelytyped/strict-export-declare-modifiers, @typescript-eslint/no-unsafe-function-type
33
type GlobalFunction = Function;
44
declare module "../index" {
55
type FunctionBase = GlobalFunction;

‎types/lodash/common/string.d.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ declare module "../index" {
2929
* @param string The string to capitalize.
3030
* @return Returns the capitalized string.
3131
*/
32-
capitalize(string?: string): string;
32+
capitalize<T extends string>(string?: T): Capitalize<Lowercase<T>>;
3333
}
3434
interface LoDashImplicitWrapper<TValue> {
3535
/**
3636
* @see _.capitalize
3737
*/
38-
capitalize(): string;
38+
capitalize(): Capitalize<Lowercase<TValue extends string ? TValue : never>>;
3939
}
4040
interface LoDashExplicitWrapper<TValue> {
4141
/**
4242
* @see _.capitalize
4343
*/
44-
capitalize(): StringChain;
44+
capitalize(): StringChain<Capitalize<Lowercase<TValue extends string ? TValue : never>>>;
4545
}
4646

4747
interface LoDashStatic {

‎types/lodash/fp.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ declare namespace _ {
240240
type LodashBindKey1x1 = (key: string) => (...args: any[]) => any;
241241
type LodashBindKey1x2 = (object: object) => (...args: any[]) => any;
242242
type LodashCamelCase = (string: string) => string;
243-
type LodashCapitalize = (string: string) => string;
243+
type LodashCapitalize = <T extends string>(string: T) => Capitalize<Lowercase<T>>;
244244
type LodashCastArray = <T>(value: lodash.Many<T>) => T[];
245245
type LodashCeil = (n: number) => number;
246246
interface LodashChunk {

‎types/lodash/lodash-tests.ts‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6623,10 +6623,27 @@ fp.now(); // $ExpectType number
66236623

66246624
// _.capitalize
66256625
{
6626-
_.capitalize("fred"); // $ExpectType string
6627-
_("fred").capitalize(); // $ExpectType string
6628-
_.chain("fred").capitalize(); // $ExpectType StringChain<string>
6629-
fp.capitalize("fred"); // $ExpectType string
6626+
_.capitalize("fred"); // $ExpectType "Fred"
6627+
_.capitalize("FRED"); // $ExpectType "Fred"
6628+
_.capitalize("fred" as string); // $ExpectType Capitalize<Lowercase<string>>
6629+
// @ts-expect-error cannot assign non string type
6630+
_.capitalize(123);
6631+
6632+
_("fred").capitalize(); // $ExpectType "Fred"
6633+
_("FRED").capitalize(); // $ExpectType "Fred"
6634+
_("fred" as string).capitalize(); // $ExpectType Capitalize<Lowercase<string>>
6635+
_(123).capitalize(); // $ExpectType never
6636+
6637+
_.chain("fred").capitalize(); // $ExpectType StringChain<"Fred">
6638+
_.chain("FRED").capitalize(); // $ExpectType StringChain<"Fred">
6639+
_.chain("fred" as string).capitalize(); // $ExpectType StringChain<Capitalize<Lowercase<string>>>
6640+
_.chain(123).capitalize(); // $ExpectType StringChain<never>
6641+
6642+
fp.capitalize("fred"); // $ExpectType "Fred"
6643+
fp.capitalize("FRED"); // $ExpectType "Fred"
6644+
fp.capitalize("fred" as string); // $ExpectType Capitalize<Lowercase<string>>
6645+
// @ts-expect-error cannot assign non string type
6646+
fp.capitalize(123);
66306647
}
66316648

66326649
// _.deburr

0 commit comments

Comments
 (0)