2

I've run my code through tsc, and have the following output for a constructor function accepting a single string parameter:

function Session(endpoint) {
    var _this = this;
    _super.call(this);
    this.endpoint = endpoint;

There's no check that the parameter is defined or that typeof(endpoint) === "string".

Are these checks only made at compile time? I originally had some sanity checks in the constructor to ensure that the value was correct, then deleted them because I assumed tsc would output them. Type declarations will certainly be useful when compiling, but I was hoping for runtime checks as well, and hoping not to have to write all that boilerplate.

Thanks.

2 Answers 2

3

No. Typescript does not have runtime type checks.

A proposition to include this in the language was declined with a reference to their design goals.

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

Comments

1

Should have known to post a question before my first cup of coffee. Googling "typescript runtime type checks" gave me the answer:

It is apparently outside of TS' goals but there is an NPM package that does it.

Comments

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.