node-odbc icon indicating copy to clipboard operation
node-odbc copied to clipboard

[TypeScript] Change API types from class to interface

Open dwickern opened this issue 1 year ago • 4 comments

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
  }
}

dwickern avatar Nov 08 '24 23:11 dwickern

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

kadler avatar Nov 15 '24 01:11 kadler

@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) {

worksofliam avatar Nov 15 '24 15:11 worksofliam

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

dwickern avatar Nov 15 '24 17:11 dwickern

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

worksofliam avatar Nov 21 '24 20:11 worksofliam