diff --git a/index.d.ts b/index.d.ts index c5fdb78..3e9dd33 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,3 +1,39 @@ +// Source: https://github.com/sindresorhus/type-fest/blob/main/source/internal.d.ts#L49 +type Whitespace = + | '\u{9}' // '\t' + | '\u{A}' // '\n' + | '\u{B}' // '\v' + | '\u{C}' // '\f' + | '\u{D}' // '\r' + | '\u{20}' // ' ' + | '\u{85}' + | '\u{A0}' + | '\u{1680}' + | '\u{2000}' + | '\u{2001}' + | '\u{2002}' + | '\u{2003}' + | '\u{2004}' + | '\u{2005}' + | '\u{2006}' + | '\u{2007}' + | '\u{2008}' + | '\u{2009}' + | '\u{200A}' + | '\u{2028}' + | '\u{2029}' + | '\u{202F}' + | '\u{205F}' + | '\u{3000}' + | '\u{FEFF}'; + +// Source: https://github.com/sindresorhus/type-fest/blob/main/source/trim.d.ts +type TrimStart = S extends `${Whitespace}${infer R}` ? TrimStart : S; + +type TrimEnd = S extends `${infer R}${Whitespace}` ? TrimEnd : S; + +export type Trim = TrimStart>; + /** Trim from the start and end of a string. @@ -9,7 +45,7 @@ trimNewlines('\nšŸ¦„\r\n'); //=> 'šŸ¦„' ``` */ -export function trimNewlines(string: string): string; +export function trimNewlines(string: S): Trim; /** Trim from the start of a string. @@ -22,7 +58,7 @@ trimNewlines.start('\nšŸ¦„\r\n'); //=> 'šŸ¦„\r\n' ``` */ -export function trimNewlinesStart(string: string): string; +export function trimNewlinesStart(string: S): TrimStart; /** Trim from the end of a string. @@ -35,4 +71,4 @@ trimNewlines.end('\nšŸ¦„\r\n'); //=> '\nšŸ¦„' ``` */ -export function trimNewlinesEnd(string: string): string; +export function trimNewlinesEnd(string: S): TrimEnd; diff --git a/index.test-d.ts b/index.test-d.ts index 49b07a1..635c88f 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,16 @@ -import {expectType} from 'tsd'; +import {expectType, expectAssignable} from 'tsd'; import {trimNewlines, trimNewlinesStart, trimNewlinesEnd} from './index.js'; -expectType(trimNewlines('\nšŸ¦„\r\n')); -expectType(trimNewlinesStart('\n\nšŸ¦„\n')); -expectType(trimNewlinesEnd('\nšŸ¦„\n\n')); +expectAssignable(trimNewlines('\nšŸ¦„\r\n')); +expectAssignable(trimNewlinesStart('\n\nšŸ¦„\n')); +expectAssignable(trimNewlinesEnd('\nšŸ¦„\n\n')); + +expectType<'šŸ¦„'>(trimNewlines('\nšŸ¦„\r\n')); +expectType<'šŸ¦„\n'>(trimNewlinesStart('\n\nšŸ¦„\n')); +expectType<'\nšŸ¦„'>(trimNewlinesEnd('\nšŸ¦„\n\n')); + +declare const _string: string; + +expectType(trimNewlines(_string)); +expectType(trimNewlinesStart(_string)); +expectType(trimNewlinesEnd(_string));