Civet 发布 TypeScript 超集 以更少键入换取更多类型

原标题:Civet: TypeScript superset that favors more types and less typing
58AI 研判分

Civet 是一个面向 TypeScript 的现代化超集语言,主打“多类型、少键入”的设计哲学,通过更简洁的语法编译为标准 TypeScript。项目提供 Vite、esbuild、Babel、Jest、Bun 等主流工具链插件,并附带 VSCode 扩展、在线 Playground 与 Solid 模板,降低上手门槛。

关联知识实体

报道正文

本文目录5 个章节

Civet

The modern way to write TypeScript.

Quickstart Guide

BASH
# Install
npm install -g @danielx/civet
# Run Civet code directly in a REPL
civet
# Transpile typed Civet code into TypeScript in a REPL
civet -c
# Compile Civet source file to TypeScript
civet < source.civet > output.ts
# Execute a .civet script
civet source.civet ...args...
# Execute a .civet source file in node
node --import @danielx/civet/register source.civet
# Type check entire project, which requires TypeScript installed
npm install -g typescript
civet --typecheck

image

Code Sample

TYPESCRIPT
ts, {CompilerOptions} from typescript

DefaultCompilerOptions : CompilerOptions :=
  allowNonTsExtensions: true
  allowJs: true
  target: ts.ScriptTarget.Latest
  moduleResolution: ts.ModuleResolutionKind.NodeJs
  module: ts.ModuleKind.CommonJS
  allowSyntheticDefaultImports: true
  experimentalDecorators: true

fileCache : Record<string, any> := {}

createCompilerHost := (options: CompilerOptions, moduleSearchLocations : string[]) ->
  fileExists := (fileName: string) : boolean ->
    fileCache[fileName]?

  readFile := (fileName: string) ->
    fileCache[fileName]

Overview

Civet is essentially a tasteful superset of TypeScript.

Implementations of New and Proposed ES Features

See the documentation for examples of these and other features.

  • Pattern matching (based on TC39 proposal)
    • switch can match patterns like [{type: "text", name}, ...rest]
  • Pipe operator (based on F# pipes, Hack pipes and the TC39 proposal)
    • data |> Object.keys |> console.log equivalent to console.log(Object.keys(data))
    • Use single-argument arrow functions or & shorthand to specify how to use left-hand side
    • |> await, |> yield, and |> return (at end) for wrapping left-hand side with that operation
  • Short function block syntax like Ruby symbol to proc, Crystal, Elm record access
    • Access: x.map &.name or x.map .name → x.map(a => a.name)
    • Nested access + slices: x.map &.profile?.name[0...3] → x.map(a => a.profile?.name.slice(0, 3))
    • Function call: x.map &.callback a, b → x.map($ => $.callback(a, b))
    • Unary operators: x.map !!& → x.map($ => !!$)
    • Binary operators: x.map &+1 → x.map($ => $+1)
  • Object literal shorthand
    • {foo()} → {foo: foo()}, {props.foo} → {foo: props.foo}
    • {`${x}${y}`: z} → {[`${x}${y}`]: z}
    • data.{x,y} or data{x,y} → {x: data.x, y: data.y}
    • Flagging shorthand based on from LiveScript: {+debug, -live, !verbose} → {debug: true, live: false, verbose: false}
  • Custom infix operators from any two-argument function
  • do expressions, if expressions, for expressions

Convenience for ES6+ Features

  • Const assignment shorthand: a := b → const a = b, {a, b} := c → const {a, b} = c
  • Let assignment shorthand: a .= b → let a = b
  • Typed versions of above: a: number .= 5 → let a: number = 5 (but note that a: number = 5 is the object literal {a: (number = 5)}).
  • @#id → this.#id shorthand for private identifiers
  • import shorthand: x from ./x → import x from "./x"
  • Dynamic import shorthand: import './x' not at top level (e.g. await import './x' or inside a function) → import('./x')
  • Optional import rename syntax that corresponds to destructuring rename import {x: y} from "./z" → import {x as y} from "./z". You can still use as to be compatible with existing ES imports.
  • export shorthand: export x, y → export {x, y}
  • Triple backtick Template Strings remove leading indentation for clarity
  • Class constructor shorthand @( ... )
  • ClassStaticBlock @ { ... }
  • < as extends shorthand
  • /// Block RegExp like Python re.X

(注:更多技术实现细节与完整 API 文档请查阅原项目 README)