简体   繁体   English

打字稿:***编译时***验证两种类型是否相等?

[英]Typescript: ***Compile-time*** validation that two types are equal?

I'm looking to have the equivalent of C static_assert() , but only usable against compile-time data, such as type information (which isn't emitted).我正在寻找与 C static_assert()等效的东西,但仅可用于编译时数据,例如类型信息(未发出)。

I'd like something similar to the following... 我想要类似以下的东西......
 type TStrLit_0 = 'a' | 'b' | 'C' | 'd'; type TStrLit_1 = 'a' | 'b' | 'C' | 'd'; type TStrLit_2 = 'a' | 'b' | 'c' | 'd'; type TStrLit_3 = 'a' | 'b' | 'C' ; type TStrLit_4 = 'a' | 'b' | 'C' |'d' | 'e'; static_assert( TypesAreEqual<TStrLit_0, TStrLit_1>() ); static_assert( !TypesAreEqual<TStrLit_0, TStrLit_1>() ); static_assert( !TypesAreEqual<TStrLit_0, TStrLit_1>() ); static_assert( !TypesAreEqual<TStrLit_0, TStrLit_1>() ); static_assert( !TypesAreEqual<TStrLit_0, TStrLit_1>() ); type TObject_0 = { a: number, b: string }; type TObject_1 = { a: number, b: string }; type TObject_2 = { a: number, b: number }; type TObject_3 = { a: number, B: string }; type TObject_4 = { a: number }; type TObject_5 = { a: number, b: string, c: number }; type TObject_6 = { a: any, b: any }; static_assert( TypesAreEqual<TObject_0, TObject_1>() ); static_assert(!TypesAreEqual<TObject_0, TObject_2>() ); static_assert(!TypesAreEqual<TObject_0, TObject_3>() ); static_assert(!TypesAreEqual<TObject_0, TObject_4>() ); static_assert(!TypesAreEqual<TObject_0, TObject_5>() ); static_assert(!TypesAreEqual<TObject_0, TObject_6>() );

What I'm looking for is the generic ability to have typescript-specific, compile-time-only assertions based on the type information.我正在寻找的是基于类型信息具有特定于打字稿的、仅编译时的断言的通用能力。

The compiler already generates the type information, but it's not emitted and thus any runtime assertions have to first change the source to emit extra metadata.编译器已经生成了类型信息,但它没有被发出,因此任何运行时断言都必须首先更改源以发出额外的元数据。

(eg, for string literals, generating the type from an as const string array, adding typeguard functions, out-of-band tracking the relationship between those string literal types and the source string arrays, ... ugh!) (例如,对于字符串文字,从as const字符串数组生成类型,添加类型保护函数,带外跟踪这些字符串文字类型和源字符串数组之间的关系,......呃!)

Having compile-time type information assertions would allow embedding assumed invariants into the code, saving future maintainers pain when they unknowingly violate those invariants.拥有编译时类型信息断言将允许将假定的不变量嵌入到代码中,从而在将来的维护人员在不知不觉中违反这些不变量时避免他们的痛苦。

Does anything like this already exist?类似的东西已经存在了吗?

Would this require changes to the TypeScript core, or can it be done with existing functionality?这是否需要对 TypeScript 核心进行更改,还是可以使用现有功能来完成?

Thanks!谢谢!

Starting with the links in early comments(*), I came up with the following:从早期评论(*)中的链接开始,我想出了以下内容:

type TypesAreEqual2<T, U> = [T] extends [U] ? [U] extends [T] ? true : false : false;

This appears to work for all the (admittedly simple) test cases I could come up with in 20 minutes:这似乎适用于我可以在 20 分钟内提出的所有(诚然简单的)测试用例:

See https://tsplay.dev/wRGDnNhttps://tsplay.dev/wRGDnN

(*) majusebetter pointed to a similar question that jcalz answered in 2018. Alex Wayne provided a tsplayground link. (*) majusebetter 指出了 jcalz 在 2018 年回答的一个类似问题。Alex Wayne 提供了一个 tsplayground 链接。 Neither of those solutions was exactly what I was looking for, but both were helpful in getting me the results I was looking for.这些解决方案都不是我正在寻找的,但它们都有助于我获得我正在寻找的结果。 Thanks to you both!谢谢你们俩!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM