Skip to content

Releases: eggjs/egg-ts-helper

1.33.0

13 Sep 10:47
Compare
Choose a tag to compare

What's Changed

Full Changelog: 1.32.0...1.33.0

1.30.3 / 2022-04-24

24 Apr 03:41
Compare
Choose a tag to compare

fixes

2019-03-25, Version 1.25.0

25 Mar 07:43
Compare
Choose a tag to compare
  • feat: config improvement.

support dot-prop in configuration

{
  "name": "app",
  "egg": {
    "tsHelper": {
      "watchDirs.model.enabled": false,
      "watchDirs.service": false,
      "watchDirs.dal": {
        "interface": "IDAL2"
      }
    }
  }
}
  • fix: directory of custom loader support array.
  • feat: optimize AutoInstanceType type

1.24.0

11 Mar 08:14
Compare
Choose a tag to compare

Custom Loader

Now egg-ts-helper support customLoader configuration of egg. see eggjs/egg#3480

Download this repo to have a try : https://github.com/whxaxes/egg-boilerplate-d-ts

Configure in config.default.ts

'use strict';

import { EggAppConfig, PowerPartial } from 'egg';

export default function(appInfo: EggAppConfig) {
  const config = {} as PowerPartial<EggAppConfig>;

  config.keys = appInfo.name + '123123';

  config.customLoader = {
    model: {
      directory: 'app/model',
      inject: 'app',
      caseStyle: 'upper',
    },
  };

  return {
    ...config as {},
    ...bizConfig,
  };
}

egg-ts-helper will auto create the d.ts for files under app/model

// This file is created by [email protected]
// Do not modify this file!!!!!!!!!

import 'egg';
type AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;
import ExportCastle from '../../../app/model/Castle';
import ExportUser from '../../../app/model/User';

declare module 'egg' {
  interface Application {
    model: T_custom_model;
  }

  interface T_custom_model {
    Castle: AutoInstanceType<typeof ExportCastle>;
    User: AutoInstanceType<typeof ExportUser>;
  }
}

And you can easily to use it in your code.

image

Load all plugins

Auto load all plugins to d.ts like below

// typings/config/plugin.d.ts
// This file is created by [email protected]
// Do not modify this file!!!!!!!!!

import 'egg';
import 'egg-onerror';
import 'egg-session';
import 'egg-i18n';
import 'egg-watcher';
import 'egg-multipart';
import 'egg-security';
import 'egg-development';
import 'egg-logrotator';
import 'egg-schedule';
import 'egg-static';
import 'egg-jsonp';
import 'egg-view';
import 'egg-router-plus';
import { EggPluginItem } from 'egg';
declare module 'egg' {
  interface EggPlugin {
    onerror?: EggPluginItem;
    session?: EggPluginItem;
    i18n?: EggPluginItem;
    watcher?: EggPluginItem;
    multipart?: EggPluginItem;
    security?: EggPluginItem;
    development?: EggPluginItem;
    logrotator?: EggPluginItem;
    schedule?: EggPluginItem;
    static?: EggPluginItem;
    jsonp?: EggPluginItem;
    view?: EggPluginItem;
    routerPlus?: EggPluginItem;
  }
}

Then you can write plugin.ts like this

// config/plugin.ts

import { EggPlugin } from 'egg';

const plugin: EggPlugin = {};

plugin.static = true;

export default plugin;

So you can get the best experiences of intelliSense!

image

Is that cool ?

1.22.0

14 Feb 15:42
Compare
Choose a tag to compare

jsconfig generation

Auto generate jsconfig.json in project if egg-ts-helper/register was running in js project, It will not generate file if jsconfig.json has already exists.

Why?

Because the IntelliSense only works in js project which has jsconfig.json or tsconfig.json in vscode.

env options

Support passing options from env, here is the supported env list

  • ETS_CWD
  • ETS_FRAMEWORK
  • ETS_TYPINGS
  • ETS_CASE_STYLE
  • ETS_AUTO_REMOVE_JS
  • ETS_THROTTLE
  • ETS_WATCH
  • ETS_EXEC_AT_INIT
  • ETS_SILENT
  • ETS_CONFIG_FILE

1.21.0

01 Feb 16:15
Compare
Choose a tag to compare

Auto create d.ts to create a global namespace: Egg, so the types can be used without import.

The created d.ts is

// typings/app/index.d.ts
export * from 'egg';
export as namespace Egg;

In js project, we can use Egg.Application instead of import('egg').Application.

/**
 * @param {Egg.Application} app
 */
module.exports = app => {
  const { router, controller } = app;
  router.get('/', app.middleware.access, controller.home.index);
}

In ts project, we can use Egg.Application and no need to import Application from egg.

export default (app: Egg.Application) => {
  const { router, controller } = app;
  router.get('/', app.middleware.access, controller.home.index);
}

1.20.0

06 Jan 15:57
Compare
Choose a tag to compare

Init command

$ npx ets init

It will create tsconfig.json or jsconfig.json in your project( If there is no config file ), And add egg-ts-helper/register to egg.require in package.json( need [email protected] ) like below.

  "egg": {
    "require": [
      "egg-ts-helper/register"
    ]
  },

Extension of config file default to .js or .json

In tshelper.js

// {cwd}/tshelper.js

module.exports = {
  watch: true,
  execAtInit: true,
  watchDirs: {
    model: {
      enabled: true,
      generator: "function",
      interfaceHandle: "InstanceType<{{ 0 }}>"
    },
  }
}

In tshelper.json

// {cwd}/tshelper.json

{
  "watch": true,
  "execAtInit": true,
  "watchDirs": {
    "model": {
      "enabled": true,
      "generator": "function",
      "interfaceHandle": "InstanceType<{{ 0 }}>"
    },
  }
}

Support silent in configuration.

// {cwd}/tshelper.json

{
  "watch": true,
  "silent": true
}

Custom generator support default config

// ./my-generator.js

module.exports.defaultConfig = {
  // default watchDir config
}

// custom generator
module.exports = (config, baseConfig) => {
  // config.dir       dir
  // config.dtsDir    d.ts dir
  // config.file      changed file
  // config.fileList  file list
  console.info(config);
  console.info(baseConfig);

  // return type can be object or array { dist: string; content: string } | Array<{ dist: string; content: string }>
  // egg-ts-helper will remove dist file when content is undefined.
  return {
    dist: 'd.ts file url',
    content: 'd.ts content'
  }
}

1.19.0

12 Dec 11:16
Compare
Choose a tag to compare

Add a new generator auto

the types created by auto generator like below. It will check types automatically.

type AutoInstanceType<T, U = T extends (...args: any[]) => any ? ReturnType<T> : T> = U extends { new (...args: any[]): any } ? InstanceType<U> : U;

interface IModel {
  Station: AutoInstanceType<typeof Station>;
}

suitable for module in below.

export default class XXXController extends Controller { }
export default () => {
  return {};
}
export default {}

Generator of configure can be file path

define generator to other js.

// ./my-generator.js

// custom generator
module.exports = (config, baseConfig) => {
  // config.dir       dir
  // config.dtsDir    d.ts dir
  // config.file      changed file
  // config.fileList  file list
  console.info(config);
  console.info(baseConfig);

  // return type can be object or array { dist: string; content: string } | Array<{ dist: string; content: string }>
  // egg-ts-helper will remove dist file when content is undefined.
  return {
    dist: 'd.ts file url',
    content: 'd.ts content'
  }
}

configure in tshelper.js or package.json

// ./tshelper.js

module.exports = {
  watchDirs: {
    model: {
      path: 'app/model',
      generator: './my-generator',
      trigger: ['add', 'unlink'],
    }
  }
}

1.18.0

10 Dec 06:14
Compare
Choose a tag to compare

interfaceHandle can be string

The type of interfaceHandle can be string

// ./tshelper.js

module.exports = {
  watchDirs: {
    model: {
      ...

      interfaceHandle: '{{ 0 }} & { [key: string]: any }',
    }
  }
}

or in package.json

{
  ...
  "egg": {
    "tsHelper": {
      "watchDirs": {
        "model": {
          "interfaceHandle": "{{ 0 }} & { [key: string]: any }",
        }
      }
    }
  }
  ...
}

{{ 0 }} means the first argument in interfaceHandle function.

The generated typings.

interface IModel {
  Station: Station & { [key: string]: any };
}

1.17.0

04 Dec 16:05
Compare
Choose a tag to compare

Support js project

the d.ts created in js project

// This file is created by egg-ts-helper
// Do not modify this file!!!!!!!!!

import 'egg';
import ExportHome = require('../../../app/controller/home');

declare module 'egg' {
  interface IController {
    home: ExportHome;
  }
}

see https://github.com/whxaxes/egg-ts-helper/tree/master/test/fixtures/real-js/ to know more.

Add oneForAll option

how to use oneForAll

$ npx ets -o

and then will created typings/ets.d.ts which is includes all d.ts.

eg.

// This file is created by egg-ts-helper
// Do not modify this file!!!!!!!!!

import './app/extend/context';
import './app/extend/helper';
import './app/controller/index';
import './app/middleware/index';
import './config/index';
import './config/plugin';
import './app/service/index';