Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com> Co-authored-by: Misha Kaletsky <15040698+mmkal@users.noreply.github.com>
- Loading branch information
1 parent
8bd5d27
commit 2f6aa0b
Showing
9 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
Extracts the type of the last element of an array. | ||
Use-case: Defining the return type of functions that extract the last element of an array, for example [`lodash.last`](https://lodash.com/docs/4.17.15#last). | ||
@example | ||
``` | ||
import {LastArrayElement} from 'type-fest'; | ||
declare function lastOf<V extends any[]>(array: V): LastArrayElement<V>; | ||
const array = ['foo', 2]; | ||
typeof lastOf(array); | ||
//=> number | ||
``` | ||
*/ | ||
export type LastArrayElement<ValueType extends unknown[]> = | ||
ValueType extends [infer ElementType] | ||
? ElementType | ||
: ValueType extends [infer _, ...infer Tail] | ||
? LastArrayElement<Tail> | ||
: never; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import {expectType} from 'tsd'; | ||
import {LastArrayElement} from '../ts41'; | ||
|
||
declare function lastOf<V extends [any, ...any]>(array: V): LastArrayElement<V>; | ||
const array: ['foo', 2, 'bar'] = ['foo', 2, 'bar']; | ||
const mixedArray: ['bar', 'foo', 2] = ['bar', 'foo', 2]; | ||
|
||
expectType<'bar'>(lastOf(array)); | ||
expectType<2>(lastOf(mixedArray)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {expectType} from 'tsd'; | ||
import {Split} from '../ts41'; | ||
|
||
declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>; | ||
|
||
const items = 'foo,bar,baz,waldo'; | ||
const array = split(items, ','); | ||
|
||
expectType<'foo'>(array[0]); | ||
expectType<'bar'>(array[1]); | ||
expectType<'baz'>(array[2]); | ||
expectType<'waldo'>(array[3]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {expectType} from 'tsd'; | ||
import {Trim} from '../ts41'; | ||
|
||
declare function trim<S extends string>(value: S): Trim<S>; | ||
|
||
expectType<'foo'>(trim(' foo')); | ||
expectType<'bar'>(trim('bar ')); | ||
expectType<'baz'>(trim(' baz ')); | ||
expectType<'waldo'>(trim(' waldo ')); | ||
expectType<'fr ed'>(trim(' fr ed ')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
Represents an array of strings split using a given character or character set. | ||
Use-case: Defining the return type of a method like `String.prototype.split`. | ||
@example | ||
``` | ||
import {Split} from 'type-fest'; | ||
declare function split<S extends string, D extends string>(string: S, separator: D): Split<S, D>; | ||
type Item = 'foo' | 'bar' | 'baz' | 'waldo'; | ||
const items = 'foo,bar,baz,waldo'; | ||
let array: Item[]; | ||
array = split(items, ','); | ||
``` | ||
*/ | ||
export type Split<S extends string, D extends string> = | ||
S extends `${infer T}${D}${infer U}` | ||
? [T, ...Split<U, D>] | ||
: [S]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
Remove spaces from the left side. | ||
*/ | ||
type TrimLeft<V extends string> = V extends ` ${infer R}` ? TrimLeft<R> : V; | ||
|
||
/** | ||
Remove spaces from the right side. | ||
*/ | ||
type TrimRight<V extends string> = V extends `${infer R} ` ? TrimRight<R> : V; | ||
|
||
/** | ||
Remove leading and trailing spaces from a string. | ||
@example | ||
``` | ||
import {Trim} from 'type-fest'; | ||
Trim<' foo '> | ||
//=> 'foo' | ||
``` | ||
*/ | ||
export type Trim<V extends string> = TrimLeft<TrimRight<V>>; |