1

I'm using a lot of implicit any in TypeScript. I don't quite understand how to decipher a TSD file in order to get the correct type when building off a library like express or angular. Is there a way to learn what the type of something is at runtime, so that I can go back and fill in the type later? Or is there another way of figuring out types based on existing code?

  • 3
    Ok, what tools and type definitions are you using? Visual Studio is working pretty good for me, and shows type information during editing. – Erik Eidt Feb 12 '16 at 2:01
  • @ErikEidt I'm using vim for editing, gulp for precompiling, and tsd for downloading type definitions. I'm doing most of my development on linux. I tried Visual Studio Code, but I had some trouble configuring it correctly. I've never really gotten into an IDE before, since I use primarily Ruby/JavaScript it isn't really necessary. I know additional IDE support is a feature of TypeScript, but I was hoping to avoid learning a new software package. If you're suggestion is that using an IDE is the best way to get type information, I guess it can't be helped. – Shaun Feb 12 '16 at 13:44
  • I still don't know if there is a way to actually output the type names so that you can go back and fill in later. But I got better and reading TSD files and switched to using VSCode for JavaScript/TypeScript. It's a much more lightweight editor than the full Visual Studio package and still provides some very helpful inspection tools for working with JavaScript and TypeScript. Though it's vim mode plugin seems a little buggy. – Shaun Aug 19 '18 at 16:42
0

Is there a way to learn what the type of something is at runtime, so that I can go back and fill in the type later?

TypeScript uses type erasure and does not run at runtime:

Current proposals for adding gradual typing to JavaScript, such as Closure, TypeScript and Dart, forgo soundness to deal with issues of scale, code reuse, and popular programming patterns.

Use an alternative, such as the following:

JS++® introduces the first and only type system compatible with JavaScript that is optional and "sound."

In other words, if you choose to declare the type for a variable, it is guaranteed to always be correct — during compile-time checking and runtime execution — even if you're using "untyped" JavaScript libraries.

JavaScript offers five ways: typeOf and instanceOf, isPrototypeOf, constructor, and Object.getPrototypeOf:

typeof 1;

/foo/ instanceof RegExp

Error.isPrototypeOf(ReferenceError)

this.constructor

Object.getPrototypeOf(NaN)

References

  • 1
    This doesn't really help in the context of TypeScript, because typeof for any complex type will just return the string "object". instanceof and isPrototypeOf require you to have some idea of what type you might expect already. – Shaun Aug 19 '18 at 16:29
  • 1
    TypeScript doesn't run at runtime, so constructor checking is the only other option. – Paul Sweatte Aug 19 '18 at 19:32
  • 4
    TypeScript uses type erasure on generics, so there is no way to discover their types at runtime. – Robert Stiffler Aug 20 '18 at 15:21
0

As mentioned previously TS has model of the types only compile time. If you open the produced js files you'll get good understanding what's "left" and visible at runtime.

Your struggle seems to originate in the problem that you are using libraries whithout being able to know the types their methods return or expect, that's what I undrestand. Which IDE or editor are you using? VSCode or Atom (or even Emacs) have excellent intellisense for typescipt.

The tsd files are bit confusing and importing types is bit of a magic for me yet, but just reading the raw libfefs still could provide you with enough understanding about the most frequent types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.