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
-
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)
1. Import preset type file in a project
- Install
npm i @type-zen/preset-type -D
- 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
, ...)
- Use different tools to code according to different scenarios
See the extension to learn more
npm i @type-zen/cli -D
tzc -h
Unplugin (To be developed)
Contains
Webpack
,Vite
,Rollup
, ...
Generate .d.ts
, ...
-
Type Challenges
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 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 areturn
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 { ... } ... |
✅ |
...
And friends who have supported me~💛
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
MIT