Skip to content

Feature: Provide Babel Macro to avoid extra build step #22

@zenflow

Description

@zenflow

If we provide a "Babel Macro" (plugin for babel-plugin-macros), then users can avoid having an extra build step using the ts-interface-builder CLI.

Users could write something like this in their source:

import { getCheckers } from 'ts-interface-builder/macro'
import { ISomeConfig } from './types'

const checkers: ICheckerSuite = getCheckers('./types')

export function validateSomeConfig(config: any): ISomeConfig {
  checkers.ISomeConfig.check(config)
  return config as ISomeConfig
}
Compiled to JavaScript
import { getCheckers } from 'ts-interface-builder/macro'

const checkers = getCheckers('./types')

export function validateSomeConfig(config) {
  checkers.ISomeConfig.check(config)
  return config
}

After the babel-plugin-macros transform it would look like this:

import * as t from 'ts-interface-checker'

const checkers = t.createCheckers({
  ISomeConfig: t.iface([], {
    // ... ISomeConfig described here ...
  })
})

export function validateSomeConfig(config) {
  checkers.ISomeConfig.check(config)
  return config
}

p.s. babel-plugin-macros is a really popular plugin and comes preloaded in some frameworks' Babel configs, like Create React App and tsdx.

What do you think @dsagal ?

I'm about to try implementing this. If I get it working as I envision, will you review a PR to add the feature here?

P.S. Thank you for the incredible packages! ❤️ They are saving me a lot of repetition in my code for runtime config validation! (Repetition that would have also been verbose and ugly, using a 2nd DSL to describe types 🤮, like with AJV or Yup or Superstruct)

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