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] Change API types from class to interface #408

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dwickern
Copy link

@dwickern dwickern commented Nov 8, 2024

These types don't actually exist at runtime, or at least they are not exported.

For example, this code will compile but fail at runtime with TypeError: Right-hand side of 'instanceof' is not an object:

const odbc = require('odbc');

try {
  // run some queries
} catch (e) {
  if (e instanceof odbc.NodeOdbcError) {
    // handle error
  }
}

@kadler
Copy link
Member

kadler commented Nov 15, 2024

@worksofliam can you look at this? I'm not familiar enough with TypeScript to review this.

@worksofliam
Copy link
Member

@kadler I am not able to leave reviews, but I'd say this is partially correct and therefore requires more changes (or maybe less changes). It makes sense to convert the class signatures to interface when they have no properties which are instances methods. I wouldn't be converting Statement and Pool to use interface.

@dwickern For your comparison there, you might be better off with if ('odbcErrors' in e) {

@dwickern
Copy link
Author

Sorry, I'm not sure I understand your suggestion about instance methods.

This PR fixes the types to match the runtime behavior. Unlike interface which is a compile-time construct, a class has a runtime representation. This code should not compile because none of these things have a runtime representation:

import odbc from 'odbc'

odbc.ColumnDefinition // undefined
odbc.Result // undefined
odbc.OdbcError // undefined
odbc.NodeOdbcError // undefined
odbc.Statement // undefined
odbc.Connection // undefined
odbc.Pool // undefined
odbc.Cursor // undefined

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.

3 participants