Typescript: adding only an optional property removes type checking

I am using typescript 1.4.1 and have the following code:

var hello: {message: string}; hello = {world: 't'}; 

As expected, the typescript compiler generates an error:

Type '{world: string; } 'is not assigned to type' {message: string; } ". The 'message' property is not in the type '{world: string;}". (var) hello: {message: string; }

But if the type contains only an optional property, for example:

 var hello: {message?: string}; hello = {world: 't'}; 

Then the compiler is completely happy, but I would like the error to be detected.

What am I doing wrong? or is it a typescript error?

Thank you for your help!

+3
source share
1 answer

Then the compiler is perfectly satisfied, but I would have found an error. What am I doing wrong? or is it a typescript error?

No, this is by design. additional information is in order until the required information is required (in this case, nothing).

+4
source

All Articles