-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Non-strict type checking #7489
Comments
I am not sure i see a concrete proposal here. this all depends on #5453 anyways. |
@mhegazy Not exactly. What I'm proposing is having type matching not be eager. This is an example of what I'd like to see work, but currently errors out because it's a directly recursive type. type Nested<T> = T | Array<Nested<T>>; The current workaround is a little hackish, and not very pretty: interface NestedArray<T> extends Array<Nested<T>> {}
type Nested<T> = T | NestedArray<T>; What this would match is structures like this (it's useful for virtual DOM structures, etc.): // Using Mithril as an example
const children: Nested<VirtualNode<any>> = [
m("h2", "Task List"),
tasks.map(task => m(Task, task)),
] There's other places where such recursion would help. Although it's related to #5453 with specifically partial application ( |
I don't really know enough about TypeScript's type system to make a more concrete proposal than this, though. This change would be technically non-breaking, but I expect it would be very difficult. |
I think it's good to consider this a dupe of that, then. |
chore(TypeScript): Enable 'strictNullChecks' option This tries to enable [`--strictNullChecks` option](microsoft/TypeScript#7140) of TypeScript compiler. - [Non-nullable types by ahejlsberg · Pull Request #7140 · Microsoft/TypeScript](microsoft/TypeScript#7140) - [Non-strict type checking · Issue #7489 · Microsoft/TypeScript](microsoft/TypeScript#7489) - [[Request for feedback] Nullable types, `null` and `undefined` · Issue #7426 · Microsoft/TypeScript](microsoft/TypeScript#7426) - [Control flow based type analysis by ahejlsberg · Pull Request #8010 · Microsoft/TypeScript](microsoft/TypeScript#8010) <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/karen-irc/karen/604) <!-- Reviewable:end -->
Non-strict (i.e. sometimes eager, sometimes lazy) type checking would really help type the more functional and dynamic parts of JavaScript, such as Function.prototype.bind (it's impossible to type that properly without non-strict checking of types) and currying. And to be honest, it gets highly repetitive using the current hack of interface + type alias for recursive types like
Nested<T>
. [1]Here's an example of
curry
, with the interface hack and with non-strict type checking (this also requires rest types, which might also require non-strict resolution of them):Also, to explain
bind
, it will likely need variadic types and a way to split a variadic type as well, but that'll require non-strict type checking to correctly infer the type to even check.I'm also thinking this might reduce the number of explicit generics as well, since it does a mixture of breadth-first and depth-first, stopping when it can't infer something yet. Also, when verifying generic types themselves, it would collect metadata to verify that yes, it could theoretically check.
Yes, I know this is probably pretty difficult [2], and it does theoretically make the template system Turing-complete beyond the compiler's ability to stop that, but in practice, you can set a nesting limit to something like 10,000 or similar, which should be more than plenty for most cases. C++ compilers already do the same themselves, since Turing machines have already been implemented in C++ templates, taking advantage of variadic template arguments alone.
[1] This originally was realized and conceived in this bug and elaborated in this comment.
[2] It's literally enabling a pair of variadic types to be spread across tuples and rest parameters, which is rank-2n polymorphism. This is why
bind
is a beast to type.The text was updated successfully, but these errors were encountered: