typedash
Version:
modern, type-safe collection of utility functions
16 lines (14 loc) • 592 B
text/typescript
/**
* Ensures that the string starts with the given prefix.
* @param string The string to ensure the prefix of.
* @param prefix The prefix to ensure.
* @returns The string with the given prefix.
* @example
* ```ts
* ensurePrefix('foo', 'bar'); // 'barfoo'
* ensurePrefix('foobar', 'foo'); // 'foobar'
* ```
*/
declare function ensurePrefix<S extends string, Prefix extends string>(string: S, prefix: Prefix): EnsurePrefix<S, Prefix>;
type EnsurePrefix<S extends string, Prefix extends string> = S extends `${Prefix}${infer _Suffix}` ? S : `${Prefix}${S}`;
export { ensurePrefix };