Skip to content

Commit eee7090

Browse files
authored
refactor(types): improve capitalize types (#6212)
1 parent 7121c92 commit eee7090

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

packages/shared/src/general.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,17 @@ export const hyphenate = cacheStringFunction((str: string) =>
110110
/**
111111
* @private
112112
*/
113-
export const capitalize = cacheStringFunction(
114-
(str: string) => str.charAt(0).toUpperCase() + str.slice(1)
115-
)
113+
export const capitalize = cacheStringFunction(<T extends string>(str: T) => {
114+
return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>
115+
})
116116

117117
/**
118118
* @private
119119
*/
120-
export const toHandlerKey = cacheStringFunction((str: string) =>
121-
str ? `on${capitalize(str)}` : ``
122-
)
120+
export const toHandlerKey = cacheStringFunction(<T extends string>(str: T) => {
121+
const s = str ? `on${capitalize(str)}` : ``
122+
return s as T extends '' ? '' : `on${Capitalize<T>}`
123+
})
123124

124125
// compare whether a value has changed, accounting for NaN.
125126
export const hasChanged = (value: any, oldValue: any): boolean =>

0 commit comments

Comments
 (0)