-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
137 additions
and
57 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
import { AnyObject } from './modules/infrastructure'; | ||
import { ForceExtract } from './modules/app'; | ||
import { AnyObject, DefaultOnUnknown } from './modules/infrastructure'; | ||
import { IValidations, TestsCallback } from './readable-test-types'; | ||
|
||
declare global { | ||
function describeType(description: string, cb: () => void): void; | ||
function testType(description: string, tests: AnyObject | TestsCallback): void; | ||
function assertType<T>(): IValidations<T>; | ||
|
||
interface INTERNAL_RT_CONFIG { | ||
development: DefaultOnUnknown<ForceExtract<RT_CONFIG, 'development'>, false>; | ||
conditionWay: DefaultOnUnknown<ForceExtract<RT_CONFIG, 'conditionWay'>, 'natural'>; | ||
} | ||
|
||
interface RT_CONFIG {} | ||
|
||
type RT_CONFIG_SCHEME<T extends { | ||
development?: boolean; | ||
ifConditionWay?: 'singleLine' | 'natural' | 'explicit'; // future 2.0 | ||
conditionWay?: 'singleLine' | 'natural' | 'explicit'; // future 2.0 | ||
}> = T; | ||
} |
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 |
---|---|---|
@@ -1,18 +1,36 @@ | ||
import { ForceExtract } from '../app'; | ||
|
||
export type ConditionObject = { | ||
type Explicit = { | ||
condition: boolean; | ||
type: unknown; | ||
then: unknown; | ||
else: unknown; | ||
}; | ||
|
||
export type ConditionCaseMap = { | ||
'true': 'type'; | ||
'false': 'else'; | ||
'boolean': 'type' | 'else'; | ||
type Natural = { | ||
then: unknown; | ||
else: unknown; | ||
}; | ||
|
||
// TODO improve inference of types for generics | ||
export type IfObject<Condition extends ConditionObject> = Condition[ConditionCaseMap[`${Condition['condition']}`]]; | ||
export type ExplicitCondition<Condition> = [ForceExtract<Condition, 'condition'>] extends [false] ? ForceExtract<Condition, 'else'> : ForceExtract<Condition, 'then'>; | ||
|
||
export type NaturalCondition<Condition, Obj> = [Condition] extends [false] ? ForceExtract<Obj, 'else'> : ForceExtract<Obj, 'then'>; | ||
|
||
export type SingleLineCondition<Condition, T, F = never> = [Condition] extends [false] ? F : T; | ||
|
||
export type IfSingleLine<Condition, T, F = never> = [Condition] extends [true] ? T : F; | ||
export type ExtendsCaseMapA = { | ||
'singleLine': boolean; | ||
'natural': boolean; | ||
'explicit': Explicit; | ||
}; | ||
|
||
export type ExtendsCaseMapB = { | ||
'singleLine': unknown; | ||
'natural': Natural; | ||
'explicit': never; | ||
}; | ||
|
||
export type ExtendsCaseMapC = { | ||
'singleLine': unknown; | ||
'natural': never; | ||
'explicit': 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
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 |
---|---|---|
@@ -1,27 +1,33 @@ | ||
import { If } from './infrastructure'; | ||
import { ExplicitCondition, NaturalCondition, SingleLineCondition } from './app'; | ||
|
||
describeType('If', () => { | ||
testType('Should return TrueCase if Condition is true', [ | ||
assertType<If<true, string, number>>().equals<string>(), | ||
assertType<If<true, '1234', 1234>>().equals<'1234'>(), | ||
assertType<If<true, any, unknown>>().toBeAny(), | ||
assertType<SingleLineCondition<true, string, number>>().equals<string>(), | ||
assertType<SingleLineCondition<true, '1234', 1234>>().equals<'1234'>(), | ||
assertType<SingleLineCondition<true, any, unknown>>().toBeAny(), | ||
]); | ||
|
||
testType('Should return FalseCase if Condition is false', [ | ||
assertType<If<false, string, number>>().equals<number>(), | ||
assertType<If<false, '1234', 1234>>().equals<1234>(), | ||
assertType<If<false, any, unknown>>().toBeUnknow(), | ||
assertType<SingleLineCondition<false, string, number>>().equals<number>(), | ||
assertType<SingleLineCondition<false, '1234', 1234>>().equals<1234>(), | ||
assertType<SingleLineCondition<false, any, unknown>>().toBeUnknow(), | ||
]); | ||
|
||
testType('Should return TrueCase | FalseCase if Condition is boolean', [ | ||
assertType<If<boolean, string, number>>().equals<string | number>(), | ||
assertType<If<boolean, '1234', 1234>>().equals<'1234' | 1234>(), | ||
assertType<If<boolean, any, unknown>>().toBeAny(), // in this case any | unknown resolve it as any | ||
testType('Should return TrueCase if Condition is boolean', [ | ||
assertType<SingleLineCondition<boolean, string, number>>().equals<string>(), | ||
assertType<SingleLineCondition<boolean, '1234', 1234>>().equals<'1234'>(), | ||
assertType<SingleLineCondition<boolean, any, unknown>>().toBeAny(), | ||
]); | ||
|
||
testType('Should use condition object (variant 1)', [ | ||
assertType<If<{ condition: true; type: string; else: number }>>().equals<string>(), | ||
assertType<If<{ condition: false; type: string; else: number }>>().equals<number>(), | ||
assertType<If<{ condition: boolean; type: string; else: number }>>().equals<string | number>(), | ||
testType('Should use condition object', [ | ||
assertType<ExplicitCondition<{ condition: true; then: string; else: number }>>().equals<string>(), | ||
assertType<ExplicitCondition<{ condition: false; then: string; else: number }>>().equals<number>(), | ||
assertType<ExplicitCondition<{ condition: boolean; then: string; else: number }>>().equals<string>(), | ||
]); | ||
|
||
testType('Should use Natural condition object', [ | ||
assertType<NaturalCondition<true, { then: string; else: number }>>().equals<string>(), | ||
assertType<NaturalCondition<false, { then: string; else: number }>>().equals<number>(), | ||
assertType<NaturalCondition<boolean, { then: string; else: number }>>().equals<string>(), | ||
]); | ||
}); |
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
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
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
declare interface RT_CONFIG extends RT_CONFIG_SCHEME<{ | ||
development: true; | ||
ifConditionWay: 'natural'; | ||
}> {} |