40
loading...
This website collects cookies to deliver better user experience
let foo: number = 42;
foo = "bli";
any
is used to tell TypeScript that a variable can be of any type. A variable with the type any
means can be a string , a number or anything else!any
any
removes all type checking provided by TypeScript, which is one major reason for using TypeScript over JavaScript. By using any
, you expose yourself to issues that are difficult to trace and debug, especially once the code is deployed in production.any
for a variable type in TypeScript is similar to write JavaScript.unknown
when you actually do not know the type and want to ensure type safety. When using unknown
, you can associate all types to a variable, but you cannot assign a variable with the unknown
type to another variable with a type.This PR adds a new top type unknown
which is the type-safe counterpart of any. Anything is assignable to unknown
, but unknown
isn't assignable to anything but itself and any
without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown
without first asserting or narrowing to a more specific type.
any
, this is a totally legit code block which is problematic because it does not make use of all type checking features of TypeScript and exposes you to potential typing issues in the future.let foo: any = 42;
let bar: string = foo;
unknown
(see below) does not work since a variable with the unknown
type cannot be assigned to a variable with a type.let foo: unknown = 42;
let bar: string = foo;