-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Negated types #29317
Negated types #29317
Conversation
@@ -46,7 +46,6 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(87,5): error | |||
tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(103,9): error TS2322: Type 'Extract<keyof T, string>' is not assignable to type 'K'. | |||
Type 'string & keyof T' is not assignable to type 'K'. | |||
Type 'string' is not assignable to type 'K'. | |||
Type 'string' is not assignable to type 'K'. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not complaining, but why'd this go away?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I accidentally fixed a bug where we duplicated the elaboration - this is as the same as the line above.
First take, without having tried this out:
I do have some reservations on the feature for these reasons. If you think about our features trying to satisfy convenience, intent, and safety, then I don't know if this appropriately weighs convenience and intent. |
It works just dandy~ |
What do you mean? Yesterday you mentioned that you couldn't assign an array to a |
Nope, you can't, because you can trivially make a interface PromiseLikeArray extends Array<any> implements PromiseLike<any> { /*...*/ } so, if you go to the example, I just state that I explicitly return an array that isn't PromiseLike - that is |
Per offline feedback from @ahejlsberg I've changed from |
Can this be used as a way to restrict the potential type of unbounded types? For example, This doesn't look very useful at first glance but it can be powerful on mapped types. For example, |
Yep. That's a primary driver for 'em. |
type Exact<T extends object> = T & Partial<Record<not keyof T, never>> 🤔 |
Is there an outline of how function foo(x: unknown) {
if (typeof x === "number") {
const num: number = x;
} else {
const numNot: not number = x;
}
}
Is there a short example that demonstrates wanting a @DanielRosenwasser Do you mean something like Meta question for @DanielRosenwasser: You say:
that seem like some internal principles the TS team have for designing features? Is there a public description of these? I think it would help feature proposals if external contributors could frame their suggestions with the same language used internally. @Kovensky I'm not sure that will type-check. The type type Exact<T extends object> = T & Partial<Record<(not keyof T) & string, never>> though I'm not sure what the semantics will be for |
In this PR no negated types are produced by control flow yet; however we've talked over it and negated types make the lib type facts PR elegant to implement, since we can skip using conditionals (which don't compose well) and just filter with intersections of negated types :D
Aye, a test case with an example I pulled from a related issue: // from https://github.com/Microsoft/TypeScript/issues/4183
type Distinct<A, B> = (A | B) & not (A & B);
declare var o1: {x};
declare var o2: {y};
declare function f1(x: Distinct<typeof o1, typeof o2>): void;
f1({x: 0}); // OK
f1({y: 0}); // OK
f1({x: 0, y: 0}); // Should error
Right now they're quietly dropped (aside from filtering out mismatching concrete types), like |
@weswigham Thanks! And this PR is very cool :) Re: the object literal example. Should that not be a case where EPC raises an error? I know it doesn't right now because there is no discriminant, but it probably should. Will using negation types become the canonical way of dealing with examples like this? If not, and assuming EPC does get fixed, are there many other use-cases for the special object literal relation. |
Even if excess property checking makes defining a type like |
Good job so far, I delayed some projects to wait for this feature - for more than a year. |
Do these identities hold? Just wondering what it would take to fix #28131 |
Yes. More generally, when U extends T,
Yes.
More generally when U extends T, |
src/compiler/checker.ts
Outdated
const result = isRelatedTo(source, (target as NegatedType).type); | ||
return result === Ternary.Maybe ? Ternary.Maybe : result ? Ternary.False : Ternary.True; | ||
} | ||
// Relationship check is S ⊂ T |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really need to use this symbol? It almost sounds like a joke, but this could affect memory footprint when bootstrapping the compiler since modern engines can avoid full UTF16 representations https://blog.mozilla.org/javascript/2014/07/21/slimmer-and-faster-javascript-strings-in-firefox/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minification step will simply remove this line. The article suggests to minify the code to utilise the inline strings and also help with strings interning. I guess, v8 should not be so much different in this sense and also win from code minification.
@weswigham Seems to work, right? type UnionToInterWorker<T> = not (T extends T ? not T : never);
type UnionToInter<T> = [T] extends [never] ? never : UnionToInterWorker<T>; Not quite sure what should happen here: type NotInfer<T> = T extends not (infer U) ? U : never;
type NotString = NotInfer<string> // string extends not U ? U : never; Currently it doesn't reduce; maybe infer types should be disallowed under |
It doesn't reduce because we don't reduce reducible conditionals with |
Yep, you're right. I guess it could infer |
Love coming back to this PR about once or twice a year because of not being able to do something. |
Would support for negated types make type inference involving conditional types easier and/or more powerful? |
This experiment is pretty old, so I'm going to close it to reduce the number of open PRs. |
@sandersn Is that a "no" to negated types, or will the concept be tracked somewhere else now? If the latter, I'd like to know where so that I can subscribe to the new conversation. |
Not "no", but discussion should happen on an issue, #4196 probably, instead of a PR. PRs go stale. |
(Though admittedly the proposal in the PR description is admittedly way more concrete and detailed than anything in that issue rn, and the blockers for this are less about the syntax and behavior and more about implementation and performance + language complexity concerns, all of which are more tied to this PR specifically than the original idea) |
while I certainly can relate to wanting to keep the PR count down to a manageable number (we have the same problem over on insomnia), I would like to echo the above from @weswigham. I've been following this conversation very closely for years, since it and #29729 are the two features I am the most interested in. I guess at the end of the day as long as this PR isn't locked we can all keep talking here, but it'd be fantastic to have a better understanding of what needs to happen next for negated types to rise-the-ranks a little in the prioritization. I don't want to spam iteration plans (e.g. #49074) with my wants-and-needs... but I'm not sure what else to do at this point. I hit the need for negated types on a weekly basis. Should I just drop those use-cases in here whenever I hit them? |
@dimitropoulos #49220 addresses the scenario in #29729 in a different way. |
Why was such a basic and important feature rejected for a type system? That's a pity! |
@Autumn-one It wasn't rejected, the issue is still open: #4196 As to why it's not implemented yet? Priorities, time constraints, language constraints, limited resources. Feel free to fork the TypeScript repo and try to implement it, you'll see that it's not an easy task. |
Long have we spoken of them in hushed tones and referenced them in related issues, here they are:
Negated Types
Negated types, as the name may imply, are the negation of another type. Conceptually, this means that if
string
covers all values which are strings at runtime, a "notstring
" covers all values which are... not. We had hoped that conditional types would by and large subsume any use negated types would have... and they mostly do, except in many cases we need to apply the constraint implied by the conditional's check to it's result. In thetrue
branch, we can just intersect theextends
clause type, however in thefalse
branch we've thus far been discarding the information. This means unions may not be filtered as they should (especially when conditionals nest) and information can be lost. So that ended up being the primary driver for this primitive - it's taking what a conditional typefalse
branch implies, and allowing it to stand alone as a type.Syntax
where
T
is another type. I'm open to bikeshedding this, or even shipping without syntax available, but among alternatives (!
,~
)not
reads pretty well.Identities
These are little tricks we do on negated type construction to help speed things along (and give negations on algebraic types canonical forms).
not not T
isT
not (A | B | C | ...)
isnot A & not B & not C & not ...
not (A & B & C & ...)
isnot A | not B | not C | not ...
not unknown
isnever
not never
isunknown
not any
isany
(sinceany
is theNaN
of types and behaves as both the bottom and top)Assignability Rules
Negated types, for perhaps obvious reasons, cannot be related structurally - the only sane way to relate them is in a higher-order fashion. Thus, the rules governing these relations are very important.
not S
is related to a negated typenot T
if T is related to S.This follows from the set membership inversion that a negation implies - if normally a type
S
and a typeT
would be related ifS
is a subset ofT
, when we take the complements of those sets,not S
andnot T
, those sets share an inverse relationship to the originals.S
is related to a negated typenot T
if the intersection ofS
andT
is emptyWe want to check if for all values in S, none of those values are also in T (since if they are, S is not in the negation of T). The intersection of S and T, when simplified and evaluated, is exactly the description of the common domain of the two. If this domain is empty (
never
), then we can conclude that there is no overlap between the two and thatS
must lie withinnot T
.not S
is not related to a typeT
.A negated type describes a set of values that reaches from
unknown
to its bound, while a normal type describes values from its bound tonever
- it's impossible for a negated type to satisfy a normal typeAssignability Addendum for Fresh Object Types
Frequently we want to consider a fresh object type as a singleton type (indeed, some examples in the refs assume this) - it corresponds to one runtime value, not the bounds on a value (meaning, as a type, both its upper and lower bounds are itself). Using this, we can add one more rule that allows fresh literal types to easily satisfy negated object types.
not T
if S is not related to T.Since S is a singleton type, we can assume that so long as it's type is not in
T
, then it is innot T
.Examples
Examples of negated type usage can be found in the tests of this PR (there's a few hundred lines of them, and probably some more to come for good measure), but here's some of the common ones, pulled from the referenced issues:
Fixes #26240.
Ref #4183, #4196, #7648, #12215, #18280Allows #27711 to be cleanly fixed with a lib change (example in the tests).