예제
let a:any = 123;
let b:unknown = 123;
let c = 123; //number
console.log('a type: ' + typeof a);
console.log('b type: ' + typeof b);
console.log('c type: ' + typeof c);
//console.log('a val: ' + (a+1)); //Err: prog.ts(10,26): error TS2365: Operator '+' cannot be applied to types 'unknown' and '1'.
//console.log('b val: ' + (b+1)); //Err: prog.ts(11,26): error TS2365: Operator '+' cannot be applied to types 'unknown' and '1'.
console.log('c val: ' + (c+1));
a = c;
//c = b; //Err: prog.ts(14,1): error TS2322: Type 'unknown' is not assignable to type 'number'.
a = 'aaa';
b = 'bbb';
//c = 'ccc'; //Err prog.ts(18,1): error TS2322: Type 'string' is not assignable to type 'number'.
결과
a type: number
b type: number
c type: number
c val: 124