Closed
Description
type Bool = 'true' | 'false'
type Or<A extends Bool, B extends Bool> = A extends 'true' ? 'true' : B;
type And<A extends Bool, B extends Bool> = A extends 'true' ? B extends 'true' ? 'true' : 'false' : 'false';
type Not<T extends Bool> = T extends 'true' ? 'false' : 'true';
type Test = Not<'true'>
type Test1 = Not<'false'>
type Test3 = Not<Or<'true', 'false'>>
type Test4 = And<Not<'true'>, Not<'false'>>
function test(a: Bool): Not <typeof a> {
switch (a) {
case 'false': return 'false';
case 'true': return 'true';
}
}
The test function is supposed to return 'true' for input 'false' and vice-versa. But I don't get a error for the above implementation.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
DanielRosenwasser commentedon Nov 25, 2019
2 problems:
typeof a
is alwaysBool
, andNot<Bool>
simplifies toBool
. If you want to make it dependent on the type that will eventually be given in place ofa
, you have to make the signature generic like so:function test<T extends Bool>(a: T): Not <T>
In the future
damodharanj commentedon Nov 25, 2019
The fix works only with assertion as well as default handling. Since typescript has dependent types was trying out the theorem prover like features.
rubenpieters commentedon Nov 25, 2019
Related to #33014 and #30284 . In the prototype implementation of the former ( #33237 ), which is restricted to working with indexed access types for now, the following adapted example works as you expect (both in positive and negative cases):
Also for a different formulation of
And
:damodharanj commentedon Nov 25, 2019
The above shows error for correct case
TS Version: 3.7.2
rubenpieters commentedon Nov 25, 2019
Yes, the prototype implementation of dependent-type-like functions ( #33014 ) or things like it are not part of any TypeScript version at the moment. Hopefully in some future version functionality like this will be added to TypeScript.
typescript-bot commentedon Nov 27, 2019
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.