-
-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com> Co-authored-by: Tommy <tmitchell7@uh.edu>
- Loading branch information
1 parent
f63c343
commit b2bcc38
Showing
6 changed files
with
54 additions
and
6 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
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,31 @@ | ||
/** | ||
Represents an object with `unknown` value. You probably want this instead of `{}`. | ||
Use case: You have an object whose keys and values are unknown to you. | ||
@example | ||
``` | ||
import type {UnknownRecord} from 'type-fest'; | ||
function toJson(object: UnknownRecord) { | ||
return JSON.stringify(object); | ||
} | ||
toJson({hello: 'world'}); | ||
//=> '{"hello":"world"}' | ||
function isObject(value: unknown): value is UnknownRecord { | ||
return typeof value === 'object' && value !== null; | ||
} | ||
isObject({hello: 'world'}); | ||
//=> true | ||
isObject('hello'); | ||
//=> false | ||
``` | ||
@category Type | ||
@category Object | ||
*/ | ||
export type UnknownRecord = Record<PropertyKey, unknown>; |
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,17 @@ | ||
import {expectAssignable, expectError, expectType} from 'tsd'; | ||
import type {UnknownRecord} from '../index'; | ||
|
||
declare let foo: UnknownRecord; | ||
|
||
expectAssignable<UnknownRecord>(foo); | ||
expectAssignable<UnknownRecord>(foo = {}); | ||
expectAssignable<UnknownRecord>(foo = {bar: 'baz'}); | ||
expectAssignable<UnknownRecord>(foo = {bar: {baz: 'hello'}}); | ||
|
||
expectError(foo = []); | ||
expectError(foo = 42); | ||
expectError(foo = null); | ||
|
||
expectType<unknown>(foo.bar); | ||
// eslint-disable-next-line @typescript-eslint/dot-notation | ||
expectType<unknown>(foo['bar']); |