Skip to content

Fix Record#get type checking #1015

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

Merged
merged 2 commits into from
Nov 15, 2022
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
15 changes: 6 additions & 9 deletions packages/core/src/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function generateFieldLookup<
class Record<
Entries extends Dict = Dict,
Key extends keyof Entries = keyof Entries,
FieldLookup extends Dict<string, number> = Dict<string, number>
FieldLookup extends Dict<keyof Entries, number> = Dict<keyof Entries, number>
> {
keys: Key[]
length: number
Expand Down Expand Up @@ -187,24 +187,21 @@ class Record<
return obj
}

get<K extends Key>(key: K): Entries[K]
get (key: keyof FieldLookup | number): any

get <K extends keyof Entries = keyof Entries> (key: K): Entries[K]
get (n: number): any
/**
* Get a value from this record, either by index or by field key.
*
* @param {string|Number} key Field key, or the index of the field.
* @returns {*}
*/
get (key: string | number): any {
let index
get <K extends keyof Entries = keyof Entries> (key: K | number): any {
let index: number
if (!(typeof key === 'number')) {
index = this._fieldLookup[key]
if (index === undefined) {
throw newError(
"This record has no field with key '" +
key +
"', available key are: [" +
`This record has no field with key '${key.toString()}', available keys are: [` +
this.keys.toString() +
'].'
)
Expand Down
43 changes: 42 additions & 1 deletion packages/core/test/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('Record', () => {
record.get('age')
}).toThrow(
newError(
"This record has no field with key 'age', available key are: [name]."
"This record has no field with key 'age', available keys are: [name]."
)
)
})
Expand Down Expand Up @@ -195,4 +195,45 @@ describe('Record', () => {
// Then
expect(values).toEqual(['Bob', 45])
})

it('should be able call .get() and use the field types', () => {
// Given
interface Person {
age: number
name: string
}

const record = new Record<Person>(['age', 'name'], [32, 'Dave'])

// When & Then
expect(() => {
// @ts-expect-error
record.get('something')
}).toThrow(
newError(
"This record has no field with key 'something', available keys are: [age,name]."
)
)

expect(record.get('age')).toBe(32)
expect(record.get('name')).toBe('Dave')
})

it('should be able call .get() with plain string', () => {
// Given

const record: Record = new Record(['age', 'name'], [32, 'Dave'])

// When & Then
expect(() => {
record.get('something')
}).toThrow(
newError(
"This record has no field with key 'something', available keys are: [age,name]."
)
)

expect(record.get('age')).toBe(32)
expect(record.get('name')).toBe('Dave')
})
})
15 changes: 6 additions & 9 deletions packages/neo4j-driver-deno/lib/core/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function generateFieldLookup<
class Record<
Entries extends Dict = Dict,
Key extends keyof Entries = keyof Entries,
FieldLookup extends Dict<string, number> = Dict<string, number>
FieldLookup extends Dict<keyof Entries, number> = Dict<keyof Entries, number>
> {
keys: Key[]
length: number
Expand Down Expand Up @@ -187,24 +187,21 @@ class Record<
return obj
}

get<K extends Key>(key: K): Entries[K]
get (key: keyof FieldLookup | number): any

get <K extends keyof Entries = keyof Entries> (key: K): Entries[K]
get (n: number): any
/**
* Get a value from this record, either by index or by field key.
*
* @param {string|Number} key Field key, or the index of the field.
* @returns {*}
*/
get (key: string | number): any {
let index
get <K extends keyof Entries = keyof Entries> (key: K | number): any {
let index: number
if (!(typeof key === 'number')) {
index = this._fieldLookup[key]
if (index === undefined) {
throw newError(
"This record has no field with key '" +
key +
"', available key are: [" +
`This record has no field with key '${key.toString()}', available keys are: [` +
this.keys.toString() +
'].'
)
Expand Down
4 changes: 3 additions & 1 deletion packages/neo4j-driver/test/types/record.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ const record2Get2: string[] = record2.get('age')

const record3Get1: string = record3.get('name')
const record3Get2: number = record3.get('age')
const record3Get3: any = record3.get(0)
const record3Get4: any = record3.get(1)

const record2Get3: string = record2.get('firstName')
const record2Get4: number = record2.get(1)

// @ts-expect-error
const record2Get5: any = record2.get('does-not-exist')
const record3Get5: any = record3.get('does-not-exist')