Comments (13)
#57904 (comment) is correct
T is assignable to A | B | C if there's a valid assignment from T to A, T to B, or T to C. That's it. The relationships between A, B, and C aren't considered.
TypeScript isn't a theorem prover and can't use the sort of "If it wasn't A, then it must have been B or C, and if it wasn't B, then it must have been C, therefore it's a C" logic that you're applying to reach that result.
from typescript.
The exact example given above can be easily rewritten to give the desired output, and be much simpler too:
type A = true;
let a: A = true;
Is this [] | [T] | [T, ...T]
construction something that actually comes up in code or is it just a case of "I can reason that something is true, so the type checker should be able to prove it."?
from typescript.
It seems this issue was misconstrued over the course of discussion. The purpose of this issue is to resolve the specific case when an array type is compared against a union of tuples of varying length, not for any A
, B
, and C
in A extends B | C
. The hope was that TypeScript could reduce the union, but since the assumption in #57904 (comment) is correct (i.e. TypeScript implements unions as falling into at least one basket), it seems the compiler is limited in this area. Please feel free to close the issue.
@jcalz BTW, this case is very easily solved with an interval tree. You build a tree, get the intervals, then test for some conditions.
from typescript.
Gonna be pondering whether { x: string } > { y: number }
for the purposes of that tree for the whole weekend 😉
from typescript.
@RyanCavanaugh I think there is a misunderstanding lol. The case in #57904 (comment) I was talking about is when you are comparing arrays and tuples of the same type (number[], [number], etc.
). Nothing more general than that.
from typescript.
I don't see why it should. A mutable array with arbitrary amount of elements does neither extend a tuple that's always empty, nor a tuple with always exactly one element, nor a tuple that has at least one element.
from typescript.
I don't see why it should. A mutable array with arbitrary amount of elements does neither extend a tuple that's always empty, nor a tuple with always exactly one element, nor a tuple that has at least one element.
I'm sorry, I don't understand what is being stated here. Assuming you used tuple to distinguish between resizable and fixed-sized arrays, then it's incorrect to assume [string, ...string[]]
is a tuple with at least 1 element. This can be seen with the base case [...string[]]
which string[]
extends correctly.
EDIT: Actually, I just understood your statement. It's a bit of a non-sequitur, but perhaps a well-motivated one. Of course a mutable array doesn't extend each of those types individually, but that doesn't imply it doesn't match their union. Now perhaps TypeScript implements unions as falling into at least one basket. If that's the case, then yes, your statement is correct, but that's the reason I created this issue because mathematically-speaking string[]
extends the union.
EDIT 2: Also, note that string | string
reduces to string
, so there is some reduction. I'm not aware how far it extends, but this issue is basically saying we should reduce [] | [string] | [string, ...string[]]
to string[]
.
from typescript.
string[]
is not assignable to[]
string[]
is not assignable to[string]
string[]
is not assignable to[string, ...string[]]
- Therefore,
string[]
is not assignable to the union of those three types.
Union and intersection are best thought of as disjunction/conjunction on assignability. If a value is not assignable to any member of a union individually, it's not assignable to the union. It's similar to set union in this regard.
Also, [string, ...string[]]
is indeed a tuple type (in terms of the TS type system); it's an entirely different class of type from a regular array type like string[]
. The existence of variable-length "tuples" is mostly motivated by the need to model rest parameters in function signatures. So the distinction is explicit; it's not simply a matter of fixed- vs. variable-length.
from typescript.
It does seem inconsistent. If, conceptually, boolean
extends true | false
(only) when the union is taken as a whole, it follows that T[]
extends [] | [T, ...T[]]
in much the same way. [] | [T, ...T[]]
could be reduced to [...T[]]
, which typescript does treat as equivalent to T[]
.
Edit: I take it back. [...T[]]
extends T[]
but not vice-versa.
Edit 2: Okay, typescript treats T[]
and [...T[]]
as equal. It treats [] | [T, ...T[]]
as a subtype of [...T[]]
. I guess [...T[]]
is being promoted to T[]
, but should a tuple be promoted to an array? This still feels inconsistent.
from typescript.
If a value is not assignable to any member of a union individually, it's not assignable to the union.
This is completely false. @snarbies came up with the very simple example of boolean extends true | false
.
from typescript.
Well to be fair, boolean
is literally just an alias for true | false
. I'm just grasping at straws trying to come up with a "conceptual union" that's not defined in terms of a union in typescript.
from typescript.
The kind of machinery needed to verify things like this in general seems like it would be unscalably expensive. Try to imagine implementing the compiler to notice this. Would it have to split every array type into a potentially unbounded union of various tuple types? Or look at every union of tuple types and try to figure out how to combine them? Would anyone have an idea of how this would happen in a scalable way? It feels like this @RyanCavanaugh Twitter thread: any straightforward analysis would quickly become impossible for all but the most trivial of cases.
from typescript.
@jun-sheaf Point taken, but since this is a type-level question (is type A assignable to type B), generally the preference is to have general rules that can be applied uniformly to any type rather than a bunch of special cases.
from typescript.
Related Issues (20)
- Type alias circularly references itself (5.4 regression) HOT 1
- error TS2385: Overload signatures must all be public, private or protected. HOT 2
- `export type * ...` statements in `.d.ts` files do not work (5.4 regression) HOT 4
- HTMLFormElement disallows symbol keys HOT 5
- TypeScript language service cannot find subclass references/implementation of mixin methods
- when using ts.getJSDocTags, the value of @type is not returned. Is there any solution? HOT 1
- Compiler allows narrower method signature than implemented interface HOT 7
- Allow overload signatures to have different access levels HOT 3
- Watcher recursively watches irrelevant directories HOT 11
- Auto-closing of tags within curly braces `{}` does not work when parent element is same tag in JSX HOT 1
- Undocumented breaking change in 5.5.0-beta for reference directives HOT 3
- 5.5.0 regression - importHelpers do not work with moduleResolution: bundler HOT 1
- 5.5.0 inheriting outDir: ${configDir} does not automatically exclude it from compilation HOT 3
- isolatedDeclarations wrongly enabled in VSCode HOT 3
- ESNext Set methods HOT 1
- isolatedDeclarations should not have warning for functions that have no return statements HOT 4
- Trivia ownership documentation seems to be incorrect
- `--isolatedDeclarations` allows generator functions HOT 1
- Allow `--noCheck` on the CLI with top-level `--build`
- Union in template literal simplifying unexpectedly HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.