The code below should throw a compiler error or am i missing something?
Code on Playground: https://www.typescriptlang.org/play
TypeScript Version:
[email protected], [email protected]
Code
type NormalType = string;
type WorksAsExpected = {a:number, c:number} | NormalType;
let works:WorksAsExpected = {a:1, c:2, d:3}
/*erro.ts(4,40): error TS2322: Type '{ a: number; c: number; d: number; }'
is not assignable to type '{ a: number; c: number; } | string'.
Object literal may only specify known properties, and 'd' does not exist in type
'{ a: number; c: number; } | string'.*/
type FunctionType = ()=>any;
type DoesntWork = {a:number, c:number} | FunctionType;
let worksTo:DoesntWork = 3
/*erro.ts(9,5): error TS2322: Type 'number' is not assignable to type
'{ a: number; c: number; } | (() => any)'.
Type 'number' is not assignable to type '() => any'.
*/
let doesntWork:DoesntWork = {a:1, c:2, d:3} //no error is displayed
Expected behavior:
The third assignment should cause a compiler error
Actual behavior:
No error is displayed
The code below should throw a compiler error or am i missing something?
Code on Playground: https://www.typescriptlang.org/play
TypeScript Version:
[email protected], [email protected]
Code
Expected behavior:
The third assignment should cause a compiler error
Actual behavior:
No error is displayed