Skip to content

Commit

Permalink
Fix wrong type in 2 part path get
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift committed Jun 7, 2019
1 parent bb6a1b4 commit 30faf25
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/globals.html
Original file line number Diff line number Diff line change
Expand Up @@ -1698,7 +1698,7 @@ <h4>T</h4>
<h4>K1<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">keyof T</span></h4>
</li>
<li>
<h4>K2<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">keyof T[K1]</span></h4>
<h4>K2<span class="tsd-signature-symbol">: </span><span class="tsd-signature-type">keyof U&lt;T[K1]&gt;</span></h4>
</li>
</ul>
<h4 class="tsd-parameters-title">Parameters</h4>
Expand Down
2 changes: 1 addition & 1 deletion src/lib/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export function get<T, K1 extends keyof T>(
* @returns the nested value in the source. Value returned is the reference of the value in source.
* Modifying this value will also modify the source value.
*/
export function get<T, K1 extends keyof T, K2 extends keyof T[K1]>(
export function get<T, K1 extends keyof T, K2 extends keyof U<T[K1]>>(
source: Optional<T>,
path: [K1, K2],
): U<T[K1]>[K2] | undefined
Expand Down
9 changes: 9 additions & 0 deletions src/test/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,13 @@ describe('get', () => {
expect(get({ a: { b: false } }, ['a', 'b'], true)).toBe(false)
expect(get({ a: { b: null } } as any, ['a', 'b'], true)).toBe(null)
})

test('is working correctly for undefinable get with 2 elements', () => {
interface A {
[key: string]: { a: number } | undefined
}

// the test is testing the typings of the get method
expect(get({} as A, ['k1', 'a'])).toBe(undefined)
})
})

0 comments on commit 30faf25

Please sign in to comment.