Skip to content

Commit

Permalink
Improve or types
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Sep 11, 2023
1 parent e93e494 commit f9741c5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ember-truth-helpers/src/helpers/or.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import truthConvert, {
Falsy,
MaybeTruthy,
TruthConvert,
} from '../utils/truth-convert.ts';
Expand All @@ -11,7 +12,7 @@ type FirstTruthy<T> = T extends [infer Item]
? Head
: TruthConvert<Head> extends false
? FirstTruthy<Tail>
: NonNullable<Head> | FirstTruthy<Tail>
: Exclude<Head, Falsy> | FirstTruthy<Tail>
: undefined;

interface OrSignature<T extends MaybeTruthy[]> {
Expand Down
13 changes: 13 additions & 0 deletions packages/ember-truth-helpers/src/utils/truth-convert.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import { isArray } from '@ember/array';

export type Falsy =
| { isTruthy: false }
| undefined
| null
| false
| 0
| -0
| 0n
| ''
| never[];

type ConvertTruthyObject<T> = 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> = T extends { isTruthy: true }
? true
: T extends { isTruthy: false }
Expand Down
3 changes: 3 additions & 0 deletions packages/ember-truth-helpers/type-tests/helpers/or.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ expectTypeOf(computeOr(foo, 1)).toEqualTypeOf(foo);

let maybeString: string | undefined;
expectTypeOf(computeOr(maybeString, 'foo')).toEqualTypeOf<string>();

const numberOrFalse = 1 as number | false;
expectTypeOf(computeOr(numberOrFalse, 'foo')).toEqualTypeOf<number | 'foo'>();

0 comments on commit f9741c5

Please sign in to comment.