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.