2

(This question is a dupe: see here)

In this question we've already discussed how to run/evaluate a string representing TypeScript code, and the solution looks like:

import { transpile } from 'typescript';
const result = eval(transpile(stringRepresentingTypescriptCode));

My issue is that this does not help me detect TypeScript errors dynamically. The following code:

console.log(transpile('const str: string = 123;'));

outputs:

var str = 123;\n

My desired result is something like:

const str: string = 123;
      ^

TS2322: Type 'number' is not assignable to type 'string'

How can I evaluate a string representing TypeScript while also receiving type-related error information?

Gershom Maes
  • 7,358
  • 2
  • 35
  • 55
  • Close voters - any feedback? I don't understand how this question could be interpreted as unfocused. – Gershom Maes Jul 14 '23 at 18:32
  • The [TS compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) provides this utility. – jsejcksn Jul 14 '23 at 18:32
  • @jsejcksn could you provide a minimal example which produces type-related error info? – Gershom Maes Jul 14 '23 at 18:34
  • [^](https://stackoverflow.com/questions/76690198/evaluate-typescript-from-string-throw-type-errors#comment135207735_76690198) Even a minimal example is not very minimal. Here's a potential duplicate Q/A that shows some opinionated strategies, but the compiler is simply not designed to work without a file system: https://stackoverflow.com/a/53764522/438273 – jsejcksn Jul 14 '23 at 18:52
  • [^](https://stackoverflow.com/questions/76690198/evaluate-typescript-from-string-throw-type-errors#comment135207708_76690198) Re: "_I don't understand how this question could be interpreted as unfocused_": Each question on SO should be very focused — on a single, atomic problem. The task of "validate a string as TypeScript source code and produce compiler diagnostics from it" is not a single problem, but **MANY** problems and solutions that, when composed, help you get to your desired objective. For better (and worse), SO is not the right site to ask certain kinds of questions. – jsejcksn Jul 14 '23 at 18:57
  • Hmmm, by that logic it appears that nearly every question on my feed at the moment is "unfocused". I think this is almost certainly a "single" and "atomic" question. – Gershom Maes Jul 14 '23 at 19:02
  • Ok, thanks for that link! I now understand the complexity. It looks like this question is a dupe – Gershom Maes Jul 14 '23 at 19:02
  • [^](https://stackoverflow.com/questions/76690198/evaluate-typescript-from-string-throw-type-errors#comment135208008_76690198): "_nearly every question on my feed at the moment is 'unfocused'_" You could be right about that. – jsejcksn Jul 14 '23 at 19:03
  • Well, I would feel very comfortable representing the argument that >50% of your own questions are also unfocused by this same logic. I would never vote to close them, though, because they seem valuable. – Gershom Maes Jul 14 '23 at 19:08
  • [^](https://stackoverflow.com/questions/76690198/evaluate-typescript-from-string-throw-type-errors?noredirect=1#comment135208063_76690198) Each of our own interests and valuations (unfortunately) aren’t open/close criteria on this site — quite a few of my questions have been voted closed (and subsequently deleted). I think that’s just the nature of social decision-making. – jsejcksn Jul 14 '23 at 20:09
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/254505/discussion-between-jsejcksn-and-gershom-maes). – jsejcksn Jul 14 '23 at 20:09

0 Answers0