4

I'm using typescript 1.4.1 and have the following code:

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

As intended, the typescript compiler outputs an error:

Type '{ world: string; }' is not assignable to type '{ message: string; }'. Property 'message' is missing in type '{ world: string; }'. (var) hello: { message: string; }

But if the type contains only an optional property such as in:

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

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

What am'I doing wrong? or is it a typescript bug?

Thanks for your help!

1 Answer 1

4

Then the compiler is perfectly happy, but I would the error to be detected. What am'I doing wrong? or is it a typescript bug?

No this is by design. extra information is okay as long as the required information (in this case nothing) is present.

Sign up to request clarification or add additional context in comments.

2 Comments

OK thanks, indeed! But I think this is very misleading when you have an interface full of optional properties (say HighChartsOptions for instance) and you make a typo. Typescript compiler does nothing for you in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.