TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Sets, types and type checking

118 点作者 kaleidawave7 个月前

9 条评论

haileys6 个月前
&gt; <i>In Rust we have Option&lt;T&gt;, which is equivalent to T | null</i><p>No, not true!<p>As the author correctly states earlier in the post, unions are not an exclusive-or relation. Unions are often made between disjoint types, but <i>not always</i>.<p>This becomes important when T itself is nullable. Let&#x27;s say T is `U | null`. `Option&lt;Option&lt;U&gt;&gt;` in Rust has one more inhabitant than `U | null | null` in TypeScript - `Some(None)`.<p>Union types can certainly be very useful, but they are tricky because they don&#x27;t compose like sum types do. When writing `Option&lt;T&gt;` where T is a generic type, we can treat T as a totally opaque type - T could be anything and the code we write will still be correct. On the other hand, union types pierce this opacity. The meaning of `T | null` and the meaning of code you write with such a type <i>does</i> depend on what T is at runtime.
评论 #42020509 未加载
评论 #42018579 未加载
评论 #42032409 未加载
评论 #42032469 未加载
评论 #42019214 未加载
nikeee7 个月前
An honorable mention is the string template literal type. It is between the string literal type (-unions); which allow a finite set of strings and the string type which, in theory, is an infinite set of strings. Template literal types can be infinite as well, but only represent a fraction. For example `foo${string}` represent all strings that start with &quot;foo&quot;.<p>Similar to this, I proposed inequality types for TS. They allow constraining a number range. For example, it is possible to have the type &quot;a number that is larger than 1&quot;. You can combine them using intersection types, forming intervals like &quot;a number between 0 and 1&quot;. Because TS has type narrowing via control flow, these types automatically come forward if you do an &quot;if (a&lt;5)&quot;. The variable will have the type &quot;(&lt;5)&quot; inside the if block.<p>You can find the proposal here [1]. Personally I think the effort for adding these isn&#x27;t worth it. But maybe someone likes it or takes it further.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;43505">https:&#x2F;&#x2F;github.com&#x2F;microsoft&#x2F;TypeScript&#x2F;issues&#x2F;43505</a>
评论 #42016537 未加载
评论 #42015163 未加载
o11c7 个月前
`never` is better known as `bottom`. `noreturn` in some languages is the same thing<p>`any`, however, is not `top`, it is `break_the_type_system`. The top type in TS is `unknown`.
评论 #42013064 未加载
评论 #42014436 未加载
throwaway17_177 个月前
I don’t think it is appropriate to say Rust has ‘union types’. Rust has sum types, implemented as Enums and (unsafe) Union types. There is a distinct difference between sum types and union types from a type theoretic perspective.
评论 #42013282 未加载
评论 #42017159 未加载
评论 #42020524 未加载
James_K6 个月前
I think this post should have made a greater distinction between terms and types. The type 4 and the term 4 are separate entities with the same name. Otherwise string &lt;=: string would imply that a function f : string =&gt; int could be called as f(string) on the type of strings (which is actually how they handle it Perl6&#x2F;Raku). Overall it seemed very TS-centric and not much on type theory.
skybrian7 个月前
&gt; Like sets, types can be by description have an infinite number of distinct entries<p>I think they might have meant &quot;entities&quot; instead of &quot;entries?&quot;<p>The term &quot;diagonal identity&quot; seems to be non-standard as well?
评论 #42011980 未加载
disqard6 个月前
Could someone here shed some light on &quot;Why the intersection with any is always any&quot;?<p>I didn&#x27;t feel satisfied with the explanation in TFA.
Mathnerd3146 个月前
&gt; Free variables and closures<p>Are these even types? I always mentally filed closures under &quot;implementation detail of nested functions&quot;.
ingen0s6 个月前
Finally, something useful to read