RepositoryTypeScriptUSHoa Kỳ
paarthenon

paarthenon/variant

Variant types in TypeScript

193sao
3fork
14issue
2lượt xem

🧩 Variant — Variant types cho TypeScript

Variant types (hay discriminated unions) trong TypeScript thường gây boilerplate và dễ lỗi khi dùng thuần. Variant cung cấp công cụ kiểu an toàn, tránh boilerplate.

Variant là một thư viện TypeScript mang khái niệm variant type (tức union có phân biệt — discriminated unions) từ ngôn ngữ như OCaml/ReasonML vào TypeScript. Mỗi trường hợp của variant có thể chứa dữ liệu kèm theo, phù hợp để mô tả các mô hình miền linh hoạt hoặc DSL nhỏ.

✨ Điểm nổi bật

  • Variant type an toàn: mỗi case có thể mang dữ liệu, được kiểu hóa chặt chẽ.
  • Fluent dispatching: cung cấp công cụ để xử lý từng case một cách rõ ràng, an toàn.
  • Giảm boilerplate: tránh viết thủ công các hàm so khớp và kiểm tra kiểu.
  • Dành cho DSL/mô hình miền: phù hợp mô tả logic phức tạp một cách khai báo.
  • Tích hợp tốt với TypeScript: dựa trên hệ thống kiểu TypeScript, hỗ trợ tốt với công cụ.

💡 Vì sao đáng chú ý

Thay vì dùng union thô trong TypeScript — dễ lỗi và lặp đi lặp lại — Variant cung cấp một lớp bọc giúp khai báo và xử lý variant một cách ngắn gọn, đồng thời giữ được tính an toàn kiểu. Đây là lựa chọn tốt cho dự án cần mô tả logic nhánh phức tạp hoặc xây DSL nhỏ trong TypeScript.


#TypeScript #VariantType #DiscriminatedUnions #TypeTheory #TiniX

Dự án tương tự

Khám phá các dự án và mô hình AI khác có sự tương đồng.

RepositoryC#
mknejp/dotvariant

A type-safe and space-efficient sum type for C# (comparable to discriminated unions in C or C++)

73
Jun 30, 2026
RepositoryTypeScript
n8n-io/n8n

Fair-code workflow automation platform with native AI capabilities. Combine visual building with custom code, self-host or cloud, 400+ integrations.

213.0k
Dec 3, 2026
RepositoryTypeScript
microsoft/vscode

Visual Studio Code

195.9k
Nov 30, 2026
RepositoryJavaScript
paularmstrong/normalizr

Normalizes nested JSON according to a schema

20.8k
Mar 19, 2022
RepositoryJavaScript
kdchang/reactjs101

從零開始學 ReactJS(ReactJS 101)是一本希望讓初學者一看就懂的 React 中文入門教學書,由淺入深學習 React.js 生態系 (Flux, Redux, React Router, ImmutableJS, React Native, Relay/GraphQL etc.)。

4.3k
Oct 11, 2023
RepositoryC#
domn1995/dunet

C# discriminated union source generator

886
Nov 16, 2026
RepositoryC++
mpark/variant

C++17 `std::variant` for C++11/14/17

716
Dec 7, 2022
RepositoryC++
eggs-cpp/variant

Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union.

141
Feb 2, 2022
RepositoryC++
cbeck88/strict-variant

A realtime/embedded-friendly C++11 variant type which is never empty and prevents undesirable implicit conversions

102
Aug 8, 2018