any
tipo:
O any
tipo representa todos os valores possíveis de JS. Todo tipo é atribuível ao tipo any
. Portanto, o tipo any
é um supertipo universal do sistema de tipos. O compilador TS permitirá qualquer operação nos valores digitados any
. Por exemplo:
let myVar: any;
myVar[0];
myVar();
myVar.length;
new myVar();
Em muitas ocasiões, isso é muito tolerante com o compilador TS. isto é, permitirá operações que poderíamos saber estar resultando em um erro de tempo de execução.
unknown
tipo:
O unknown
tipo representa (exatamente como any
) todos os valores possíveis de JS. Todo tipo é atribuível ao tipo unknown
. Portanto, o tipo unknown
é outro supertipo universal do sistema de tipos (ao lado any
). No entanto, o compilador TS não permitirá nenhuma operação nos valores digitados unknown
. Além disso, o unknown
tipo é apenas atribuível ao tipo any
. Um exemplo esclarecerá isso:
let myVar: unknown;
let myVar1: unknown = myVar; // No error
let myVar2: any = myVar; // No error
let myVar3: boolean = myVar; // Type 'unknown' is not assignable to type 'boolean'
// The following operations on myVar all give the error:
// Object is of type 'unknown'
myVar[0];
myVar();
myVar.length;
new myVar();
any
édynamic
eunknown
é comoobject
. Eu gostounknown
, pois é apenas mais seguro para o tipo. Nomes confusos.