diff --git a/packages/ember-truth-helpers/src/helpers/or.ts b/packages/ember-truth-helpers/src/helpers/or.ts index a846406..03c3c5b 100644 --- a/packages/ember-truth-helpers/src/helpers/or.ts +++ b/packages/ember-truth-helpers/src/helpers/or.ts @@ -1,4 +1,5 @@ import truthConvert, { + Falsy, MaybeTruthy, TruthConvert, } from '../utils/truth-convert.ts'; @@ -11,7 +12,7 @@ type FirstTruthy = T extends [infer Item] ? Head : TruthConvert extends false ? FirstTruthy - : NonNullable | FirstTruthy + : Exclude | FirstTruthy : undefined; interface OrSignature { diff --git a/packages/ember-truth-helpers/src/utils/truth-convert.ts b/packages/ember-truth-helpers/src/utils/truth-convert.ts index b3b9440..f617b36 100644 --- a/packages/ember-truth-helpers/src/utils/truth-convert.ts +++ b/packages/ember-truth-helpers/src/utils/truth-convert.ts @@ -1,9 +1,22 @@ import { isArray } from '@ember/array'; +export type Falsy = + | { isTruthy: false } + | undefined + | null + | false + | 0 + | -0 + | 0n + | '' + | 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 } ? true : T extends { isTruthy: false } diff --git a/packages/ember-truth-helpers/type-tests/helpers/or.test.ts b/packages/ember-truth-helpers/type-tests/helpers/or.test.ts index e58fb25..0095fac 100644 --- a/packages/ember-truth-helpers/type-tests/helpers/or.test.ts +++ b/packages/ember-truth-helpers/type-tests/helpers/or.test.ts @@ -29,3 +29,11 @@ expectTypeOf(computeOr(foo, 1)).toEqualTypeOf(foo); let maybeString: string | undefined; expectTypeOf(computeOr(maybeString, 'foo')).toEqualTypeOf(); + +const numberOrFalse = 1 as number | false; +expectTypeOf(computeOr(numberOrFalse, 'foo')).toEqualTypeOf(); + +const numberOrNotTruthy = 1 as number | { isTruthy: false }; +expectTypeOf(computeOr(numberOrNotTruthy, 'foo')).toEqualTypeOf< + number | 'foo' +>();