-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #196 from wagenet/better-types
Improve types for `and` and `or`
- Loading branch information
Showing
13 changed files
with
267 additions
and
27 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
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,6 +1,5 @@ | ||
import truthConvert from '../utils/truth-convert.ts'; | ||
import type { MaybeTruth } from '../utils/truth-convert.ts'; | ||
import truthConvert, { MaybeTruthy } from '../utils/truth-convert.ts'; | ||
|
||
export default function not(...params: MaybeTruth[]) { | ||
export default function not(...params: MaybeTruthy[]) { | ||
return params.every((param) => !truthConvert(param)); | ||
} |
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
33 changes: 33 additions & 0 deletions
33
packages/ember-truth-helpers/type-tests/helpers/and.test.ts
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import AndHelper from '../../src/helpers/and'; | ||
import { MaybeTruthy } from '../../src/utils/truth-convert'; | ||
|
||
import { expectTypeOf } from 'expect-type'; | ||
|
||
function computeAnd<T extends MaybeTruthy[]>(...params: T) { | ||
const and = new AndHelper<T>(); | ||
return and.compute(params); | ||
} | ||
|
||
expectTypeOf(computeAnd()).toEqualTypeOf<undefined>(); | ||
|
||
expectTypeOf(computeAnd(undefined)).toEqualTypeOf<undefined>(); | ||
expectTypeOf(computeAnd(null)).toEqualTypeOf<null>(); | ||
expectTypeOf(computeAnd(1, false, null)).toEqualTypeOf<false>(); | ||
const stringEnum: 'foo' | 'bar' = 'foo'; | ||
expectTypeOf( | ||
computeAnd(undefined, null, stringEnum) | ||
).toEqualTypeOf<undefined>(); | ||
expectTypeOf(computeAnd(stringEnum, stringEnum)).toEqualTypeOf(stringEnum); | ||
expectTypeOf(computeAnd(1, true, [])).toEqualTypeOf<never[]>(); | ||
expectTypeOf(computeAnd({ isTruthy: true })).toEqualTypeOf<{ | ||
isTruthy: true; | ||
}>(); | ||
expectTypeOf(computeAnd({ isTruthy: true }, 1)).toEqualTypeOf<1>(); | ||
expectTypeOf(computeAnd({ isTruthy: false }, 1)).toEqualTypeOf<{ | ||
isTruthy: false; | ||
}>(); | ||
|
||
const foo: { isTruthy: true } = { isTruthy: true }; | ||
expectTypeOf(computeAnd(foo, 1)).toEqualTypeOf<1>(); | ||
|
||
expectTypeOf(computeAnd({}, [])).toEqualTypeOf<never[]>(); |
28 changes: 28 additions & 0 deletions
28
packages/ember-truth-helpers/type-tests/helpers/or.test.ts
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import OrHelper from '../../src/helpers/or'; | ||
import { MaybeTruthy } from '../../src/utils/truth-convert'; | ||
|
||
import { expectTypeOf } from 'expect-type'; | ||
|
||
function computeOr<T extends MaybeTruthy[]>(...params: T) { | ||
const or = new OrHelper<T>(); | ||
return or.compute(params); | ||
} | ||
|
||
expectTypeOf(computeOr()).toEqualTypeOf<undefined>(); | ||
|
||
expectTypeOf(computeOr(undefined)).toEqualTypeOf<undefined>(); | ||
expectTypeOf(computeOr(null)).toEqualTypeOf<null>(); | ||
expectTypeOf(computeOr(1, false, null)).toEqualTypeOf<1>(); | ||
const stringEnum: 'foo' | 'bar' = 'foo'; | ||
expectTypeOf(computeOr(undefined, null, stringEnum)).toEqualTypeOf(stringEnum); | ||
expectTypeOf(computeOr(1, true, [])).toEqualTypeOf<1>(); | ||
expectTypeOf(computeOr({ isTruthy: true })).toEqualTypeOf<{ | ||
isTruthy: true; | ||
}>(); | ||
expectTypeOf(computeOr({ isTruthy: true }, 1)).toEqualTypeOf<{ | ||
isTruthy: true; | ||
}>(); | ||
expectTypeOf(computeOr({ isTruthy: false }, 1)).toEqualTypeOf<1>(); | ||
|
||
const foo: { isTruthy: true } = { isTruthy: true }; | ||
expectTypeOf(computeOr(foo, 1)).toEqualTypeOf(foo); |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../tsconfig.json", | ||
"include": [ | ||
"./**/*", | ||
"../unpublished-development-types/**/*" | ||
] | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/ember-truth-helpers/type-tests/utils/truth-convert.test.ts
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 |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import truthConvert from '../../src/utils/truth-convert'; | ||
import type { TruthConvert } from '../../src/utils/truth-convert'; | ||
import { expectTypeOf } from 'expect-type'; | ||
|
||
expectTypeOf<TruthConvert<null>>().toEqualTypeOf<false>(); | ||
expectTypeOf<TruthConvert<undefined>>().toEqualTypeOf<false>(); | ||
|
||
expectTypeOf<TruthConvert<true>>().toEqualTypeOf<true>(); | ||
expectTypeOf<TruthConvert<false>>().toEqualTypeOf<false>(); | ||
|
||
expectTypeOf<TruthConvert<0>>().toEqualTypeOf<false>(); | ||
expectTypeOf<TruthConvert<-0>>().toEqualTypeOf<false>(); | ||
expectTypeOf<TruthConvert<0n>>().toEqualTypeOf<false>(); | ||
expectTypeOf<TruthConvert<1>>().toEqualTypeOf<true>(); | ||
|
||
expectTypeOf<TruthConvert<''>>().toEqualTypeOf<false>(); | ||
expectTypeOf<TruthConvert<'A String'>>().toEqualTypeOf<true>(); | ||
|
||
expectTypeOf<TruthConvert<{ foo: 1; isTruthy: true }>>().toEqualTypeOf<true>(); | ||
expectTypeOf< | ||
TruthConvert<{ foo: 1; isTruthy: false }> | ||
>().toEqualTypeOf<false>(); | ||
// isTruthy isn't a boolean but we still have a real object so it's truthy | ||
expectTypeOf<TruthConvert<{ foo: 1; isTruthy: 1 }>>().toEqualTypeOf<true>(); | ||
|
||
expectTypeOf<TruthConvert<never[]>>().toEqualTypeOf<false>(); | ||
expectTypeOf<TruthConvert<string[]>>().toEqualTypeOf<boolean>(); | ||
|
||
expectTypeOf< | ||
TruthConvert<never[] & { isTruthy: true }> | ||
>().toEqualTypeOf<true>(); | ||
|
||
expectTypeOf(truthConvert(null)).toEqualTypeOf<false>(); | ||
expectTypeOf(truthConvert(undefined)).toEqualTypeOf<false>(); | ||
|
||
expectTypeOf(truthConvert(true)).toEqualTypeOf<true>(); | ||
expectTypeOf(truthConvert(false)).toEqualTypeOf<false>(); | ||
|
||
expectTypeOf(truthConvert(0)).toEqualTypeOf<false>(); | ||
expectTypeOf(truthConvert(-0)).toEqualTypeOf<false>(); | ||
expectTypeOf(truthConvert(0n)).toEqualTypeOf<false>(); | ||
// We can't enumerate every number | ||
expectTypeOf(truthConvert(1)).toEqualTypeOf<true>(); | ||
|
||
expectTypeOf(truthConvert('' as const)).toEqualTypeOf<false>(); | ||
// We can't enumerate every string | ||
expectTypeOf(truthConvert('A String')).toEqualTypeOf<true>(); | ||
|
||
expectTypeOf(truthConvert({ foo: 1, isTruthy: true })).toEqualTypeOf<true>(); | ||
expectTypeOf(truthConvert({ foo: 1, isTruthy: false })).toEqualTypeOf<false>(); | ||
// isTruthy isn't a boolean but we still have a real object so it's truthy | ||
expectTypeOf(truthConvert({ foo: 1, isTruthy: 1 })).toEqualTypeOf<true>(); | ||
|
||
const neverArray: never[] = []; | ||
expectTypeOf(truthConvert(neverArray)).toEqualTypeOf<false>(); | ||
// We don't know that it's empty | ||
const emptyStringArray: string[] = []; | ||
expectTypeOf(truthConvert(emptyStringArray)).toEqualTypeOf<boolean>(); | ||
|
||
const neverArrayWithTruthy = [] as never[] & { isTruthy: true }; | ||
neverArrayWithTruthy.isTruthy = true; | ||
expectTypeOf(truthConvert(neverArrayWithTruthy)).toEqualTypeOf<true>(); |
5 changes: 4 additions & 1 deletion
5
packages/modern-test-app/app/components/and-or-type-checking.hbs
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,7 @@ | ||
<div> | ||
{{log @andArg}} | ||
{{log @andArg1}} | ||
{{log @andArg2}} | ||
{{log @andArg3}} | ||
{{log @andArg4}} | ||
{{log @orArg}} | ||
</div> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.