Skip to content

Typescript: Constants for field types not available at runtime #347

@RungryCoder

Description

@RungryCoder

Hi,

in the current 3.5.2 version, the constants for field types are defined as enum in file types/share.d.ts:

export enum Types {
  DECIMAL = 'DECIMAL',
  TINY = 'TINY',
  ...
}

However, when using Typescript, these constants are only available at compile time but not at runtime causing errors like the following when accessing e.g., Types.TINY:

TypeError: Cannot read properties of undefined (reading 'TINY')

I think the problem is caused by c8fbc69 with the change in types/share.d.ts from

export const enum Types {
...
}

to

export enum Types {
...
}

With the previous const definition, the enum values got inlined during compilation of package consumers from typescript to javascript (see typescript docs). Without the const keyword in the current version, there is no inlining and the mariadb package does not export the enum in its Javascript files.
In the Javascript files, the constants are defined in lib/const/field-type.js but with a slightly different name (all uppercase):

export const TYPES = typeNames;

Hence they cannot be used from Typescript-consumers of the package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions