How does type "Unknown" work in Typescript?

Asked

Viewed 404 times

6

The guy unknown is a Feature a little more recent language, and is said as a any, only that type safe.

So I was left with some doubts:

  • What is your difference from any?
  • It is safe to use it for type security?
  • 1

    I want to see the day that Typescript will become a "truth" language or at least do something at runtime and not at "compile" time, because I remember where I used it (I can’t remember which version) types passed in parameters and arguments do not always work as expected at runtime, of course it should all be documented and talking about it, but I think a lot will still depend on the real JS.

  • 2

    It doesn’t make much sense to compile TS for Wasm, Until and initially I found interesting, but has problems. It can leave the language without the JS layer and run faster, it could even allow some things in the language that does not give now, but would make it incompatible with transpilation for JS. There is a difficulty to use the browser backend by Wasm so you can’t get out using it without thinking about it. The advantage of TS is that it does not need it, it uses the prepared browser backend p/o JS, until pq is JS. They are different objectives. Vixi almost turned to answer and I did not detail :)

1 answer

7


My understanding of this is that the any works like the dynamic C# (I already explain to those who don’t know the language) and the unknown is the object, or almost this.

With the any You can try to access any member of the object and the language leaves, until at execution if the member is not available to the object there will be an error, and so there is no type security. At the time you execute it knows if you can access, but not before. You can receive any value in an object of type any and can store this object any in any other object.

let valor: any;
valor = 1;
let numero : number = valor;
valor = "SOpt";
valor.toUpperCase();
valor = [];
valor = {};
//valor.toUpperCase(); //tire o comentário para ver o erro em tempo de execução
console.log(numero);

I put in the Github for future reference.

The error there occurs because in this last line an object does not have the function toUpperCase(), but when the object was a string worked the call.

Already the unknown does not let you access any member of the object and because of this any attempt to access will cause error (it is a little different from C# that the object lets access some members of the type object). There is also the difference that it can receive any object but cannot be assigned to any object, there needs to be compatibility.

And you must still have a question what good is then a guy who won’t let you access anything. Here comes the type security while maintaining the flexibility of the language. You can transport types in a "generic" way, but you can only use them if you’re sure that you can access the content that’s there, so only if you test that capability before does it let you do some accessing. There are some ways to prove that it is safe to access a member of the object, I’ll show you two:

let valor: unknown;
valor = 1;
//let numero : number = valor; //isso dá erro em compilação
let numero : number = valor as number;
console.log(valor); //compila e funciona
valor = "SOpt";
//valor.toUpperCase(); //isso daria erro em compilação
if (typeof valor === "string") console.log(valor.toUpperCase()); //compila e funciona
valor = [];
valor = {};

Behold working in the repl it..

Rotate in the Playground.

Only when the code can prove the type is correct does it allow access to the members of the object. So it’s safe to use because the compiler only lets it pass when it ensures it won’t break the typing.

This can help: What is typing style?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.