Giter Site home page Giter Site logo

xlboy / type-zen Goto Github PK

View Code? Open in Web Editor NEW
147.0 3.0 5.0 968 KB

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

Home Page: https://type-zen-playground.vercel.app

License: MIT License

TypeScript 94.54% Nearley 5.35% HTML 0.09% JavaScript 0.02%
type-language typescript type-generator type-zen typezen

type-zen's Introduction

type-zen

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

English | 简体中文

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-image

preview-1

See the extension to learn more

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

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

type-zen's People

Contributors

martin-yin avatar winmezzz avatar xlboy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

type-zen's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.