4

Suppose, I am writing a library with Typescript. There is a function with following signature-

function check(value: "YES"|"NO"): boolean

So, when this function is called from other typescript files with values other than "YES" or "NO", there will be a compilation error. But if called from a Javascript file, there will be no error, as Javascript do not has the type information. I can check for invalid values inside my function and throw errors. But then the type safety provided by Typescript seems only an illusion to me.

What should I do in this case as a library developer? Go with pure javascript? What did the teams like Angular do?

7
  • You should do what you think best. This question has no objective answer, and so is off-topic for SO. "...answers to this question will tend to be almost entirely based on opinions..." Commented Jul 19, 2016 at 10:38
  • I am not to familiar with Typescript but from what I understand, it gets converted into pure javascript at compilation time. That said, if you deliver compiled code instead of typescript code, it should work as intended? I am also holding on to this point, typescript is meant to help you code by putting on restrictions and preventing errors. The JS it compiles into is what actually goes into the product. So, since you cannot force the society to use typescript you cannot really on typescript to keep your code 'error free'. Commented Jul 19, 2016 at 10:43
  • @T.J.Crowder Should I ask this on programmers? Commented Jul 19, 2016 at 10:47
  • 1
    Typescript doesn't have run-time checks, so if you plan to use your library as non-typescript and parameters check is important for it, make sure you include those restrictions in the function itself, because it's no longer Typescript domain. Commented Jul 19, 2016 at 10:51
  • @Gulshan: Possibly, I don't know what's on-topic for Programmers. I'd check their on-topic page. Commented Jul 19, 2016 at 10:51

1 Answer 1

2

This is the difference between build-time (compile-time) and run-time error checking. TypeScript only helps you with Build-time checks. Providing a TypeScript Definition File with your library will help you users get meaningful compile-time errors when they use your lib incorrectly.

If your lib is being consumed by JavaScript directly, you'll have no build-step to notify the user, and you'll have to resort to run-time messaging. If file size is not an issue, I'd suggest throwing a meaningful error message:

function (check) {
  if (check != "YES" && check != "NO") 
    throw new Error("Invalid Check Value: " + check);
  ...
}

If you are concerned about file size, probably best to simply no-op on invalid calls. Or have some sort of sensible default. It will depend on your situation.

You could also consider a "debug" build of your library that provides error messages, but exclude them from the minified release build.

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

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.