Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make JsonRecord type safe #1693

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/modules/Json.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Added in v2.10.0

```ts
export interface JsonRecord {
readonly [key: string]: Json
readonly [key: string]: Json | undefined
}
```

Expand Down
14 changes: 10 additions & 4 deletions dtslint/ts3.5/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import * as E from '../../src/Either'
import { pipe } from '../../src/function'
import * as _ from '../../src/Json'

declare const jr: _.JsonRecord

//
// JsonRecord
//

// $ExpectError
const foo1: _.Json = jr.foo
const foo2: _.Json | undefined = jr.foo

//
// stringify
//
Expand All @@ -12,10 +22,6 @@ _.stringify<_.Json>(undefined)
_.stringify<_.Json>(() => {})
// $ExpectError
_.stringify<_.Json>(Symbol())
// $ExpectError
_.stringify<_.Json>({ a: undefined })
// $ExpectError
_.stringify<_.Json>({ ...{ a: undefined } })

// tslint:disable-next-line: interface-over-type-literal
interface AB {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type Json = boolean | number | string | null | JsonArray | JsonRecord
* @since 2.10.0
*/
export interface JsonRecord {
readonly [key: string]: Json
readonly [key: string]: Json | undefined
}

/**
Expand Down
1 change: 1 addition & 0 deletions test/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe('Json', () => {

it('stringify', () => {
U.deepStrictEqual(pipe({ a: 1 }, _.stringify), E.right('{"a":1}'))
U.deepStrictEqual(pipe({ a: undefined }, _.stringify), E.right('{}'))
const circular: any = { ref: null }
circular.ref = circular
U.deepStrictEqual(
Expand Down