这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions sources/Any/If.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
import {Boolean} from '../Boolean/_Internal'

/**
* Conditional if (behaves like the JS one)
* @param B Condition
* @param Then
* @param Else (?=`'never'`)
* @example
* ```ts
* import {A, B} from 'ts-toolbelt'
*
* type test0 = A.If<B.True, string, number> // string
* type test1 = A.If<B.False, string, number> // number
* type test1 = A.If<B.False, string> // never
* type test1 = A.If<B.True, string> // string
* ```
*/
export type If<B extends Boolean, Then, Else = never> =
B extends 1
? Then
Expand Down
1 change: 1 addition & 0 deletions sources/Any/_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export {Promise} from './Promise'
export {Try} from './Try'
export {Type} from './Type'
export {x} from './x'
export {If} from './if'
95 changes: 54 additions & 41 deletions tests/Any.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
import {Test, A} from '../sources'
import {Test, A, B} from '../sources'

const {checks, check} = Test

// ///////////////////////////////////////////////////////////////////////////////////////
// ANY ///////////////////////////////////////////////////////////////////////////////////

type O = {
a: string,
b: number;
c: {a: 'a'} & {b: 'b'};
d?: 'string0';
readonly e?: 'string1';
readonly f: 0;
g: O;
h?: 1;
j: 'a' | undefined;
k: {a: {b: string}};
x: () => 1;
a: string;
b: number;
c: { a: 'a' } & { b: 'b' };
d?: 'string0';
readonly e?: 'string1';
readonly f: 0;
g: O;
h?: 1;
j: 'a' | undefined;
k: { a: { b: string } };
x: () => 1;
};

type T = [
1,
2,
'3' | undefined,
'xxxx',
{a: 'a'} & {b: 'b'},
string | number,
number,
object,
readonly [0, 1, 2?],
'xxxx'?
1,
2,
'3' | undefined,
'xxxx',
{ a: 'a' } & { b: 'b' },
string | number,
number,
object,
readonly [0, 1, 2?],
'xxxx'?
];

// ---------------------------------------------------------------------------------------
// AT

checks([
check<A.At<O, 'a'>, string, Test.Pass>(),
check<A.At<O, 'c'>, {a: 'a'} & {b: 'b'}, Test.Pass>(),
check<A.At<O, 'c'>, { a: 'a' } & { b: 'b' }, Test.Pass>(),
check<A.At<O, 'g'>, O, Test.Pass>(),
check<A.At<T, '1'>, 2, Test.Pass>(),
check<A.At<T, '3'>, 'xxxx', Test.Pass>(),
check<A.At<{a: 1} | {b: 2}, 'a'>, 1 | undefined, Test.Pass>(),
check<A.At<{ a: 1 } | { b: 2 }, 'a'>, 1 | undefined, Test.Pass>(),
])

// ---------------------------------------------------------------------------------------
// CAST

checks([
check<A.Cast<[0, 1, 2], any>, [0, 1, 2], Test.Pass>(),
check<A.Cast<{a: string}, {}>, {a: string}, Test.Pass>(),
check<A.Cast<{ a: string }, {}>, { a: string }, Test.Pass>(),
check<A.Cast<string, object>, object, Test.Pass>(),
check<A.Cast<any, string>, any, Test.Pass>(),
check<A.Cast<0, 42>, 42, Test.Pass>(),
Expand All @@ -74,8 +74,8 @@ checks([
check<A.Contains<any, string>, 0, Test.Pass>(),
check<A.Contains<string, any>, 1, Test.Pass>(),
check<A.Contains<{}, object>, 1, Test.Pass>(),
check<A.Contains<{a: any}, object>, 1, Test.Pass>(),
check<A.Contains<object, {a: any}>, 0, Test.Pass>(),
check<A.Contains<{ a: any }, object>, 1, Test.Pass>(),
check<A.Contains<object, { a: any }>, 0, Test.Pass>(),
check<A.Contains<any[], Array<any>>, 1, Test.Pass>(),
check<A.Contains<'a' | 'b', 'b' | 'a'>, 1, Test.Pass>(),
check<A.Contains<'b', 'b' | 'a'>, 1, Test.Pass>(),
Expand All @@ -98,8 +98,8 @@ checks([
check<A.Equals<any, string>, 0, Test.Pass>(),
check<A.Equals<string, any>, 0, Test.Pass>(),
check<A.Equals<{}, object>, 0, Test.Pass>(),
check<A.Equals<{a: any}, object>, 0, Test.Pass>(),
check<A.Equals<object, {a: any}>, 0, Test.Pass>(),
check<A.Equals<{ a: any }, object>, 0, Test.Pass>(),
check<A.Equals<object, { a: any }>, 0, Test.Pass>(),
check<A.Equals<any[], Array<any>>, 1, Test.Pass>(),
check<A.Equals<'a' | 'b', 'b' | 'a'>, 1, Test.Pass>(),
check<A.Equals<'a', 'a'>, 1, Test.Pass>(),
Expand All @@ -120,8 +120,8 @@ checks([
check<A.Extends<any, string>, 0 | 1, Test.Pass>(),
check<A.Extends<string, any>, 1, Test.Pass>(),
check<A.Extends<{}, object>, 1, Test.Pass>(),
check<A.Extends<{a: any}, object>, 1, Test.Pass>(),
check<A.Extends<object, {a: any}>, 0, Test.Pass>(),
check<A.Extends<{ a: any }, object>, 1, Test.Pass>(),
check<A.Extends<object, { a: any }>, 0, Test.Pass>(),
check<A.Extends<any[], Array<any>>, 1, Test.Pass>(),
check<A.Extends<'a' | 'b', 'b' | 'a'>, 1, Test.Pass>(),
check<A.Extends<'b', 'b' | 'a'>, 1, Test.Pass>(),
Expand All @@ -140,8 +140,8 @@ checks([

checks([
check<A.Keys<O>, keyof O, Test.Pass>(),
check<A.Keys<{a: 0} | {b: 0}>, 'a' | 'b', Test.Pass>(),
check<(keyof ({a: 0} | {b: 0})), 'a' & 'b', Test.Pass>(),
check<A.Keys<{ a: 0 } | { b: 0 }>, 'a' | 'b', Test.Pass>(),
check<keyof({ a: 0 } | { b: 0 }), 'a' & 'b', Test.Pass>(),
])

// ---------------------------------------------------------------------------------------
Expand All @@ -152,7 +152,7 @@ checks([
check<A.KnownKeys<string[]>, never, Test.Pass>(),
check<A.KnownKeys<O>, keyof O, Test.Pass>(),
check<A.KnownKeys<string[]>, never, Test.Pass>(),
check<A.KnownKeys<{[k: string]: any} & {a: any}>, 'a', Test.Pass>(),
check<A.KnownKeys<{ [k: string]: any } & { a: any }>, 'a', Test.Pass>(),
])

// ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -187,23 +187,19 @@ checks([
// ---------------------------------------------------------------------------------------
// PROMISE

checks([
check<A.Promise<Promise<1>>, A.Promise<1>, Test.Pass>(),
])
checks([check<A.Promise<Promise<1>>, A.Promise<1>, Test.Pass>()])

// ---------------------------------------------------------------------------------------
// AWAIT

checks([
check<A.Await<Promise<1>>, 1, Test.Pass>(),
])
checks([check<A.Await<Promise<1>>, 1, Test.Pass>()])

// ---------------------------------------------------------------------------------------
// TRY

checks([
check<A.Try<[0, 1, 2], any>, [0, 1, 2], Test.Pass>(),
check<A.Try<{a: string}, {}>, {a: string}, Test.Pass>(),
check<A.Try<{ a: string }, {}>, { a: string }, Test.Pass>(),
check<A.Try<string, object>, never, Test.Pass>(),
check<A.Try<any, string>, any, Test.Pass>(),
check<A.Try<0, 42, 'xxxx'>, 'xxxx', Test.Pass>(),
Expand All @@ -218,6 +214,23 @@ checks([
check<A.Type<string, 'name'>, A.Type<string, 'name'>, Test.Pass>(),
])

// ---------------------------------------------------------------------------------------
// IF

checks([
check<A.If<1, string, 'name'>, string, Test.Pass>(),
check<A.If<0, string, 'name'>, string, Test.Fail>(),

check<A.If<1, string, 'name'>, 'name', Test.Fail>(),
check<A.If<0, string, 'name'>, 'name', Test.Pass>(),

check<A.If<1, string>, string, Test.Pass>(),
check<A.If<0, string>, string, Test.Fail>(),

check<A.If<1, string>, never, Test.Fail>(),
check<A.If<0, string>, never, Test.Pass>(),
])

// ---------------------------------------------------------------------------------------
// X

Expand Down