Skip to content

Commit

Permalink
New Feature: make Record.hasOwnProperty a type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jan 7, 2020
1 parent add2ffa commit 38d78c4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 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
21 changes: 21 additions & 0 deletions dtslint/ts3.5/Record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as R from '../../src/Record'

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"
}
4 changes: 2 additions & 2 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 Down

0 comments on commit 38d78c4

Please sign in to comment.