npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

uitype

v1.0.18

Published

export ui component types

Downloads

8

Readme

自动生成组件类型

概述

解决那些问题

  1. 解决获取子对象时没有代码提示(提示子对象的具体属性名字)
  2. 解决获取子对象的类型不明确的问题
  3. 解决通过编辑器修改组件并发布后,项目代码不自动提示错误
  4. 把运行时阶段的空指针报错提前到编译阶段,这样能防止更多的逻辑错误

支持

本工具暂时只支持 fariygui 的组件类型生成

Usage

生产环境

安装

# 全局安装
npm install uitype -g

# 本地安装,可以用 npx uitype 调用
npm install uitype --save

通过 cli 指令调用

# 输出基本信息
uitype

# 编译
uitype compile -h

# 根据根目录编译
uitype compile --root 项目目录 --outFile 类型文件输出地址

# 编译 指定的包
uitype compile --root 项目目录 --outFile 类型文件输出地址 --include A B C

通过代码调用

// 引入包
import { compileProject } from "uitype/dist/compiler";
// 编译项目
compileProject("test/ui-project", {
  outFile: "test/ui-project-dist/uitype.d.ts",
});

代码实例

在项目中使用(以fairygui为例)

// 通过命令生成的 `uit.types.d.ts`文件(局部代码)
declare namespace uit {
  namespace test {
    namespace components {
      type TestView = StrictComponent<
        import("fairygui-cc").GComponent,
        {
          readonly btn: import("fairygui-cc").GButton;
          readonly cmpt: components.TestComponent;
        },
        "c1" | "c2",
        "t1" | "t2"
      >;
      type TestComponent = StrictComponent<
        import("fairygui-cc").GComponent,
        {
          readonly title: import("fairygui-cc").GTextField;
        },
        undefined,
        undefined
      >;
    }
  }
}
// 创建一个自定义的组件,并通过as来转换成生成的类型
import { UIPackage } from "fairygui-cc";
const view = UIPackage.createObject("test", "TestView") as uit.test.components.TestView;
// 当调用`getChild`时,参数会自动提示 'btn' | 'cmpt',且返回值类型非常明确
const child = view.getChild("cmpt", true);
// 因为`child`类型非常明确为`components.TestComponent`,因此后续继续有代码提示
const next = child.getChild("title", true);
// 当调用`getController`时,参数会自动提示 'c1' | 'c2'
const ctrl = view.getController("c1", true);
// 当调用`getController`时,参数会自动提示 't1' | 't2'
const trans = view.getTransition("t1", true);

// 也可以使用`Proxy`代理属性的获取
// 比如`Controller`代理,其他的代理也类似,有手就行~
export type ControllerProxy<Component> = Component extends { getController(name: infer Name, strict: true): infer R }
  ? NonNullable<Name> extends never
    ? never
    : { [P in Name extends string ? Name : never]: R }
  : never;
export function controllerProxyOf<T extends { getController(name: unknown): unknown }>(
  component: T
): ControllerProxy<T> {
  const pxy = new Proxy({} as ControllerProxy<T>, {
    get(target, p) {
      const name = p as keyof ControllerProxy<T>;
      if (name in target) {
        return target[name];
      }
      const value = component.getController(name);
      target[name] = value as ControllerProxy<T>[typeof name];
      return value;
    },
  });
  return pxy;
}

// 直接有代码提示,不用给每个控制前设置引用
const ctrls = controllerProxyOf(view);
ctrls.c1.selectedIndex = 0;
ctrls.c2.selectedIndex = 1;

// 也可以配合装饰器自动创建`Proxy`,这个很简单,有手就行

开发环境

工程安装

# 安装依赖
npm install

# 安装本地cli测试环境
npm link

# 卸载本地cli测试环境
npm unlink uitype -g

脚本命令

# 构建
npm run build

开发调试

使用vscode可以直接F5调试

自动构建:

# 开发模式
npm run dev