mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Added unknown type in typescript.html.markdown
This commit is contained in:
parent
8e94abc88b
commit
6c2f1c7132
@ -37,6 +37,11 @@ let notSure: any = 4;
|
|||||||
notSure = "maybe a string instead";
|
notSure = "maybe a string instead";
|
||||||
notSure = false; // okay, definitely a boolean
|
notSure = false; // okay, definitely a boolean
|
||||||
|
|
||||||
|
// When we are not sure about the type, and don't want to use "any", we can use "unknown" type
|
||||||
|
let unknownVariable: unknown = 'Abc'; // we don't fully know yet the type
|
||||||
|
unknownVariable = 1234; // possible
|
||||||
|
const someVariable: string = unknownVariable; // Invalid; we can't assign unknownVariable to any other type (without an explicit assertion), unlike any
|
||||||
|
|
||||||
// Use const keyword for constants
|
// Use const keyword for constants
|
||||||
const numLivesForCat = 9;
|
const numLivesForCat = 9;
|
||||||
numLivesForCat = 1; // Error
|
numLivesForCat = 1; // Error
|
||||||
|
Loading…
Reference in New Issue
Block a user