Here, I test TypeScript3.0 unkown
type.
TypeScript 3.0 introduces a new type called
unknown
that does exactly that. Much likeany
, any value is assignable tounknown
; however, unlikeany
, you cannot access any properties on values with the typeunknown
, nor can you call/construct them. Furthermore, values of typeunknown
can only be assigned tounknown
orany
.
I play with some Church eoncoding stuff, and testing unknown
type to every argument of functions, I have an error as below:
const log = (m: unknown) => {
console.log(m); //IO
return m;
};
const I = (x:unknown) => x;
const L = (x:unknown) => (y:unknown) => x;
const P = (x:unknown) => (y:unknown) => (z:Function) => z(x)(y);
//z is a binary operator Function!
const Left = L;
const Right = L(I);
log("Left Right test---------");
log(
Left("boy")("girl") // boy
);
log(
Right("boy")("girl") //TypeScript Type Error here
);
Error:
church.ts:20:9 - error TS2571: Object is of type 'unknown'.
20 Right("boy")("girl")
~~~~~~~~~~~~
Just in case, this is well-tested in vanilla JS, but I simply want to know how to resolve this error without using any
type.
Thanks.
Quite simply here I don't think you should use unknown
but rather a generic function as there are obvious relations between the argument to L
and the final return type:
const I = (x:unknown) => x;
const L = <T>(x:T) => (y:unknown) => x;
const Left = L;
const Right = L(I);
log("Left Right test---------");
log(
Left("boy")("girl") // boy
);
log(
Right("boy")("girl") //all ok
);
I would use unknown
much like any
as a last resort type when the type is not only unknown when writing the function (where we can use regular types) but also unknowable when calling the function (this is when I would generic type parameters).
If for some reason generics are not feasible the only way to get around this is with a type assertion, as you have information the type system lost in this case:
(Right("boy") as ((x:unknown)=> unknown))("girl") //all ok