Skip to content

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

@worksofliam
Copy link
Member

@dwickern Hey again. Had a look properly this and yes, it makes sense. My bad, I forgot node-odbc is not TS!

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