-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the object constraint for
NonEmptyObject<T>
(#336)
* fix the object constraint for `NonEmptyObject<T>` * refactor test for `NonEmptyObject` to new file * docs: 📄 update changeset Update .changeset/great-hounds-brush.md Co-authored-by: Alexey Berezin <2991847+Beraliv@users.noreply.github.com>
- Loading branch information
1 parent
66a6169
commit 4c45165
Showing
4 changed files
with
29 additions
and
10 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"ts-essentials": patch | ||
--- | ||
|
||
Improve the object constraint for `NonEmptyObject<T>` to not allow primitives |
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { AssertTrue as Assert, IsExact } from "conditional-type-checks"; | ||
import { NonEmptyObject } from "../lib"; | ||
|
||
function testNonEmptyObject() { | ||
type ObjectWithKeys = { foo: string; bar: number; xyz: undefined }; | ||
type EmptyObject = {}; | ||
|
||
type Cases = [ | ||
Assert<IsExact<NonEmptyObject<ObjectWithKeys>, ObjectWithKeys>>, | ||
Assert<IsExact<NonEmptyObject<EmptyObject>, never>>, | ||
// Works with Wrapper Object Types | ||
Assert<IsExact<NonEmptyObject<String>, String>>, | ||
Assert<IsExact<NonEmptyObject<Number>, Number>>, | ||
Assert<IsExact<NonEmptyObject<Boolean>, Boolean>>, | ||
// @ts-expect-error | ||
Assert<IsExact<NonEmptyObject<string>, string>>, // Will Not work as T is a Primitive | ||
// @ts-expect-error | ||
Assert<IsExact<NonEmptyObject<number>, number>>, // Will Not work as T is a Primitive | ||
// @ts-expect-error | ||
Assert<IsExact<NonEmptyObject<undefined>, number>>, // Will Not work as T is a Primitive | ||
]; | ||
} |