From f467900c97846477bc9dbcc00ea4511c01e1de20 Mon Sep 17 00:00:00 2001 From: Peter Wagenet Date: Mon, 11 Sep 2023 11:21:31 -0700 Subject: [PATCH] Improve TruthConvert accuracy --- .../ember-truth-helpers/src/utils/truth-convert.ts | 11 +++++++---- .../type-tests/utils/truth-convert.test.ts | 11 ++++++++++- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/ember-truth-helpers/src/utils/truth-convert.ts b/packages/ember-truth-helpers/src/utils/truth-convert.ts index f617b36..54cedde 100644 --- a/packages/ember-truth-helpers/src/utils/truth-convert.ts +++ b/packages/ember-truth-helpers/src/utils/truth-convert.ts @@ -11,13 +11,11 @@ export type Falsy = | '' | never[]; -type ConvertTruthyObject = T extends { isTruthy: infer U } ? U : T; - // We check here in the order of the following function to maintain parity // Note that this will not handle EmberArray correctly. // We don't use Falsy since we want to be able to more definitively determine // truthy results. -type _TruthConvert = T extends { isTruthy: true } +export type TruthConvert = T extends { isTruthy: true } ? true : T extends { isTruthy: false } ? false @@ -30,14 +28,20 @@ type _TruthConvert = T extends { isTruthy: true } : T extends number ? T extends 0 | -0 ? false + : number extends T + ? boolean : true : T extends bigint ? T extends 0n ? false + : bigint extends T + ? boolean : true : T extends string ? T extends '' ? false + : string extends T + ? boolean : true : T extends never[] ? false @@ -46,7 +50,6 @@ type _TruthConvert = T extends { isTruthy: true } : T extends object ? true : boolean; -export type TruthConvert = _TruthConvert>; export type MaybeTruthy = | { isTruthy: boolean } diff --git a/packages/ember-truth-helpers/type-tests/utils/truth-convert.test.ts b/packages/ember-truth-helpers/type-tests/utils/truth-convert.test.ts index 8bfb22d..6b33c27 100644 --- a/packages/ember-truth-helpers/type-tests/utils/truth-convert.test.ts +++ b/packages/ember-truth-helpers/type-tests/utils/truth-convert.test.ts @@ -7,14 +7,19 @@ expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); -expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf< @@ -22,6 +27,7 @@ expectTypeOf< >().toEqualTypeOf(); // isTruthy isn't a boolean but we still have a real object so it's truthy expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); expectTypeOf>().toEqualTypeOf(); @@ -30,6 +36,9 @@ expectTypeOf< TruthConvert >().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); +expectTypeOf>().toEqualTypeOf(); + expectTypeOf(truthConvert(null)).toEqualTypeOf(); expectTypeOf(truthConvert(undefined)).toEqualTypeOf();