• Stars
    star
    148
  • Rank 249,983 (Top 5 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created almost 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A language based on TypeScript type system - 一个基于 TypeScript 类型层的语言

type-zen

A language based on TypeScript type system, which solves a series of experience problems caused by writing complex type code.

English | 简体中文

HelloWorld

TypeZen:

type Without<T: unknown[], U: number | number[]> = ^{
  if (T == [infer First, ...infer Rest]) {
    type RC = U == number[] ? U[number] : U; // Right Condition

    if (First == RC) {
      return Without<Rest, U>
    } else {
      return [First, ...Without<Rest, U>]
    }
  }

  return T
}

TypeScript after conversion:

type Without<T extends unknown[], U extends number | number[]> = (
  T extends [infer First, ...infer Rest]
    ? [U extends number[] ? U[number] : U] extends [infer RC]
      ? First extends RC
        ? Without<Rest, U>
        : [First, ...Without<Rest, U>]
      : never
    : TZ_URS
) extends infer r_czl5
  ? r_czl5 extends TZ_URS
    ? T
    : r_czl5
  : never;

For more examples, please refer to Playground

Features

  • Compatible with TypeScript type syntax

  • Import and use in *.ts files via TypeScript Plugin

  • Unique syntax sugar

    • More similar to the syntax in TS/JS that is often written (understood in seconds~)

    • Writing complex type code is simpler, more efficient, and more readable

  • Write and use immediately (Playground, CLI, VSCode Extension)

How to use?

1. Import preset type file in a project
  1. Install
npm i @type-zen/preset-type -D
  1. Import in tsconfig.json
  {
    "compilerOptions": {
      "types": ["@type-zen/preset-type"]
    }
  }

PS: Why use @type-zen/preset-type as a global type file? Because the compiled TypeScript type may use some predefined types(e.g. TZ_URS , ...)


  1. Use different tools to code according to different scenarios

Playground

playground-image

VSCode Extension

preview-1

See the extension to learn more

CLI

npm i @type-zen/cli -D
tzc -h

TypeScript Plugin

preview

Unplugin (To be developed)

Contains Webpack , Vite , Rollup , ...

Generate .d.ts , ...

Tutorial & Examples

Syntax

Expression

Basic

Name Example Supported
literal number, string, ...(keyword: [any, boolean, null, never, ...])
condition a == 1 ? 1 : 2 -> a extends 1 ? 1 : 2
a extends 12 ? 13 : 233
bracket surround (123)
tuple [1, 2, 3]
array number[]
string[][]
object { a: 1, b: 2 } , ...
function (a: 1, b: 2) => 3 , ...
type-operator keyof x , readonly x , ...
infer infer x
infer xx == xxx1 -> infer xx extends xxx1
infer xx extends xxx
union 1 | 2 | 3
| [1, 2, 3]
intersection 1 & 2 & 3
& [11, 22, 33] -> 11 & 22 & 33
generic args <S: string = "S"> -> <S extends string = "S">
<A extends string = "default">
type reference A , Array<1> , IsNumber<".">
element access A["b"] , A[0][Key]
property access A.B , A.B.C
template string `hello ${name}`
⚠️ ${} expressions only support TypeScript native expressions (Does not yet support extensions such as: ^{...} , | [1, 3] , ...)
comment // ...
/* ... */

Sugar Block

Sugar Block are a special type of expression that can be used to write type logic code (if, else, for, local variable declarations, etc.)

Sugar blocks are scoped to ^{ and } , or within if,for statements.

Name Example Supported
local ^{ type B = 1; ... }
only if ^{ if (a == 1) { do something... } }
if else ^{ if (a == 1) { do something... } else { do something... } ... }
if else if ^{ if (a == 1) { do something... } else if (a == 2) { do something... } ... }
multiple condition ^{ if (a == 1 && b == 2) { do something... } ... }
^{ if (a == 1 || b == 2) { do something... } ... }
for ^{ for (infer a in UnionValue) { do something... } ... }
return ^{ ... return 1; }
switch ^{ switch (a) { case 0, case 1: do something...; case 2, case 3: do something...; } ... }

⚠️ if does not currently support != logical symbol

⚠️ In a sugar block, it must contain a return statement.

Statement

Name Example Supported
type alias type A = 1
interface interface A { b: 1 }
enum enum A { B = 1, C = "" }
const enum A { B = 1, C = "" }
namespace namespace A { ... }
declare function declare function A(): 1
declare variable declare const A: 1
declare let A: 1
declare var A: 1
declare module declare module '...' { ... }
declare global { ... } declare global { ... }
import import type {} from '...'
...
export export type { ... }
...

Issues

...

Thanks

And friends who have supported me~💛

Plan

The current stage is in the initial phase (version 0.x), with the goal of being usable and having a basic ecosystem (Playground, Cli, TS Plugin, ...)

However, there may be some shortcomings, including those related to deep implementation issues in TypeScript or limitations in the current design of @type-zen/core, among others. These shortcomings are expected to be addressed and improved in version 1.0.0

License

MIT

More Repositories

1

ts-type-hidden

[VSCode Extension] Hide those familiar type codes! - 将那些熟悉的类型代码隐藏起来吧!
TypeScript
43
star
2

bb-typewriter

一款基于Vue3+TypeScript+Kotlin开发的WebApp移动端打字练习软件,为五笔、音形文字录入爱好者而生的BB打字机。
Vue
14
star
3

swap-ternary.nvim

Easily swap the values in a ternary expression
Lua
14
star
4

react-learn-demo

初学 React 时突发奇想的一些小项目 - Some small projects that suddenly came to mind when learning React.
TypeScript
10
star
5

mimi-utils

一些实用的工具 - Some practical tools
TypeScript
9
star
6

awesome

个人的资源收集处 - Personal resource collection
7
star
7

nest-robot

一个基于node-nest框架搭起的miraiQQ机器人http服务端
TypeScript
6
star
8

vite-react-admin

TypeScript
5
star
9

flow2src

一个将流程图节点与源代码关联起来的应用(遥遥无期的进行中...)
CSS
4
star
10

vscode-opener.nvim

Open the current Neovim workspace or buffer in VSCode
Lua
4
star
11

partner-program

伙伴计划约~
TypeScript
3
star
12

learn-note

个人学习基地
TypeScript
3
star
13

workspace-scanner.nvim

[Neovim] Rich and pure project manager
Lua
3
star
14

clipboard-sync-share

可实时「跨设备同步剪贴板」的应用程序
TypeScript
2
star
15

super-hotkey

TypeScript
2
star
16

ts-morph-demo

TypeScript
2
star
17

android-kotlin-bbTypingPractice

一款手机打字练习项目,需搭配Web项目使用(隔壁库android-vue-bbTypingPractice)
Kotlin
2
star
18

weread-web-extension

微信读书网页版助手, 提供自动阅读、更改屏占比等功能
TypeScript
2
star
19

node-edge-toggler.nvim

Toggle cursor position between the start and end of a node (replaces `%`)
Lua
2
star
20

vscode-twind-intellisense

[VSCode Extension] Twind intellisense
TypeScript
2
star
21

class-string-matcher

轻松解析 `react/vue/svelte/...` 中的 class 定义
TypeScript
2
star
22

twind-typescript-plugin

Twind TypeScript Plugin
2
star
23

type-toolkit

A TypeScript type utility library developed in the TypeZen language, aims for - "Super rich features and an excellent user experience"
TypeScript
2
star
24

xlboy

1
star
25

learn-web-debug

JavaScript
1
star
26

wechat-menu-editor

Vue
1
star
27

win-mouse-positioner

C++
1
star