最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript 3.0 error on `unknown` usage - Stack Overflow

programmeradmin6浏览0评论

Here, I test TypeScript3.0 unkown type.

TypeScript 3.0 introduces a new type called unknown that does exactly that. Much like any, any value is assignable to unknown; however, unlike any, you cannot access any properties on values with the type unknown, nor can you call/construct them. Furthermore, values of type unknown can only be assigned to unknown or any.

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.

Here, I test TypeScript3.0 unkown type.

https://blogs.msdn.microsoft./typescript/2018/07/12/announcing-typescript-3-0-rc/#the-unknown-type

TypeScript 3.0 introduces a new type called unknown that does exactly that. Much like any, any value is assignable to unknown; however, unlike any, you cannot access any properties on values with the type unknown, nor can you call/construct them. Furthermore, values of type unknown can only be assigned to unknown or any.

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.

Share Improve this question edited Jul 29, 2018 at 12:43 asked Jul 29, 2018 at 12:29 user6440264user6440264 4
  • Did you expect something different? The result of L(I)("boy") is typed unknown, and the quote you include explicitly states of such types "nor can you call/construct them". – jonrsharpe Commented Jul 29, 2018 at 12:47
  • Can you clarify which quote states such types "nor can you call/construct them". – user6440264 Commented Jul 29, 2018 at 12:50
  • ...literally that one in the yellow box that you posted in your question. – jonrsharpe Commented Jul 29, 2018 at 12:51
  • @jonrsharpe , thanks, and the issue is not what I expected, but I simply want to know how to resolve this error without using any type. – user6440264 Commented Jul 29, 2018 at 12:53
Add a ment  | 

1 Answer 1

Reset to default 2

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 
发布评论

评论列表(0)

  1. 暂无评论