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

TypeScript Type Definitions #49

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

MarkHerhold
Copy link

First pass at TS types from #2

@MarkHerhold MarkHerhold mentioned this pull request Jun 13, 2020
@jeremydaly
Copy link
Owner

Thanks @MarkHerhold! Will review this soon.

@MarkHerhold
Copy link
Author

Unfortunantly, the options object is completely wrong still. I'll have to take another look at it.

@MarkHerhold
Copy link
Author

@jeremydaly Ok I figured out what you were doing with options and made some changes!

BTW, something I noticed - it's probably not a good idea to mutate global objects in this lib because you don't document the side effects and users don't expect it. IMO, it's best to leave that up to the user.

@rraziel
Copy link

rraziel commented Jun 19, 2020

Having typings support will be a nice improvement.

It would be interesting to have a generic parameter for iDataAPIQueryResult and use with query so you can do things like this without having to cast:

const result = await db.query<RecordType>('SELECT * from records');
const record = result.records[0];

Something like:

export interface iDataAPIClient {
  query<T>(sql: string, params?: [] | unknown): Promise<iDataAPIQueryResult<T>>;
  query<T>(obj: { sql: string; parameters: [] | unknown; database: string; hydrateColumnNames?: boolean }): Promise<iDataAPIQueryResult<T>>;
}

export interface iDataAPIQueryResult<T> {
  records: Array<T>;
}

@MarkHerhold
Copy link
Author

@rraziel good idea, will give it a shot 👍

}

interface iQuery<T> {
(sql: string, params?: [] | unknown): Promise<T>; // params can be [] or {}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for the generics, you can do something like this (instead of making iQuery itself a generic):

Suggested change
(sql: string, params?: [] | unknown): Promise<T>; // params can be [] or {}
<T = any>(sql: string, params?: [] | unknown): Promise<T>; // params can be [] or {}

Usage:

client.query<{ name: string; age: number }[]>('SELECT * from table';)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just realised this is the same as @rraziel 's suggestion 😅

@markpar
Copy link

markpar commented Apr 30, 2021

Anything I can do to help out on this one, @jeremydaly? The type definitions on DefinitelyTyped have what I think might be an errant type reference:

// This is added because aws-sdk depends on @types/node
/// <reference types="node" />

This is causing errors in our Reactive Native app because RN uses browser timers and referencing the node types redefines the timer functions, causing TSC errors b/c we're using number types when it now wants NodeJS.Timeout types.

@padzikm
Copy link

padzikm commented May 4, 2021

Hi, any chances that will be merged soon?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants