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 Record.hasOwnProperty a type guard #1075

Merged
merged 2 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/modules/Record.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,14 @@ Added in v2.0.0
**Signature**

```ts
export function hasOwnProperty<K extends string>(k: K, r: Record<K, unknown>): boolean { ... }
export function hasOwnProperty<K extends string>(k: string, r: Record<K, unknown>): k is K { ... }
```

Added in v2.0.0

# insertAt (function)

Insert or replace a key/value pair in a map
Insert or replace a key/value pair in a record

**Signature**

Expand Down Expand Up @@ -445,7 +445,7 @@ Added in v2.0.0
**Signature**

```ts
export function modifyAt<K extends string, A>(k: K, f: (a: A) => A): (r: Record<K, A>) => Option<Record<K, A>> { ... }
export function modifyAt<A>(k: string, f: (a: A) => A): <K extends string>(r: Record<K, A>) => Option<Record<K, A>> { ... }
```

Added in v2.0.0
Expand Down Expand Up @@ -648,7 +648,7 @@ Added in v2.0.0
**Signature**

```ts
export function updateAt<K extends string, A>(k: K, a: A): (r: Record<K, A>) => Option<Record<K, A>> { ... }
export function updateAt<A>(k: string, a: A): <K extends string>(r: Record<K, A>) => Option<Record<K, A>> { ... }
```

Added in v2.0.0
Expand Down
39 changes: 39 additions & 0 deletions dtslint/ts3.5/Record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { pipe } from '../../src/pipeable'
import * as R from '../../src/Record'
import { identity } from '../../src/function'

declare const dictionaryString: { [key: string]: number }
declare const recordString: Record<string, number>
declare const recordStringEnum: Record<'a' | 'b', number>

declare const keyString: string

//
// hasOwnProperty
//

if (R.hasOwnProperty(keyString, dictionaryString)) {
keyString // $ExpectType string
}
if (R.hasOwnProperty(keyString, recordString)) {
keyString // $ExpectType string
}
if (R.hasOwnProperty(keyString, recordStringEnum)) {
keyString // $ExpectType "a" | "b"
}

//
// updateAt
//

pipe(dictionaryString, R.updateAt('a', 3)) // $ExpectType Option<Record<string, number>>
pipe(recordString, R.updateAt('a', 3)) // $ExpectType Option<Record<string, number>>
pipe(recordStringEnum, R.updateAt('a', 3)) // $ExpectType Option<Record<"a" | "b", number>>

//
// modifyAt
//

pipe(dictionaryString, R.modifyAt('a', identity)) // $ExpectType Option<Record<string, number>>
pipe(recordString, R.modifyAt('a', identity)) // $ExpectType Option<Record<string, number>>
pipe(recordStringEnum, R.modifyAt('a', identity)) // $ExpectType Option<Record<"a" | "b", number>>
8 changes: 4 additions & 4 deletions src/Record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ export function toUnfoldable<F>(unfoldable: Unfoldable<F>): <A>(r: Record<string
}

/**
* Insert or replace a key/value pair in a map
* Insert or replace a key/value pair in a record
*
* @since 2.0.0
*/
Expand All @@ -144,7 +144,7 @@ const _hasOwnProperty = Object.prototype.hasOwnProperty
/**
* @since 2.0.0
*/
export function hasOwnProperty<K extends string>(k: K, r: Record<K, unknown>): boolean {
export function hasOwnProperty<K extends string>(k: string, r: Record<K, unknown>): k is K {
return _hasOwnProperty.call(r, k)
}

Expand All @@ -170,7 +170,7 @@ export function deleteAt(k: string): <A>(r: Record<string, A>) => Record<string,
/**
* @since 2.0.0
*/
export function updateAt<K extends string, A>(k: K, a: A): (r: Record<K, A>) => Option<Record<K, A>> {
export function updateAt<A>(k: string, a: A): <K extends string>(r: Record<K, A>) => Option<Record<K, A>> {
return r => {
if (!hasOwnProperty(k, r)) {
return none
Expand All @@ -187,7 +187,7 @@ export function updateAt<K extends string, A>(k: K, a: A): (r: Record<K, A>) =>
/**
* @since 2.0.0
*/
export function modifyAt<K extends string, A>(k: K, f: (a: A) => A): (r: Record<K, A>) => Option<Record<K, A>> {
export function modifyAt<A>(k: string, f: (a: A) => A): <K extends string>(r: Record<K, A>) => Option<Record<K, A>> {
return r => {
if (!hasOwnProperty(k, r)) {
return none
Expand Down