For example in Scala one can do following (ScalaTest):
assertDoesNotCompile("val a: String = 1")
assertTypeError("val a: String = 1")
assertCompiles("val a: Int = 1")
Does something similar exist in TypeScript world?
Edit:
I mean context-aware compilation. For example code from this question How do I write a scala unit test that ensures compliation fails?:
import shapeless.test.illTyped
//this version won't even compile
illTyped("getIdx(C.Ooga)")
//We can have multiple enum unions exist side by side
import Union_B_C._
B.values().foreach {b => Union_B_C.getIdx(b) should be (b.ordinal())}
C.values().foreach {c => Union_B_C.getIdx(c) should be (c.ordinal() + 2)}
//Though A exists in some union type, Union_B_C still doesn't know about it,
// so this won't compile
illTyped("""
A.values().foreach {a => Union_B_C.getIdx(a) should be (a.ordinal())}
""")
It is not a feature of Scala, it's a feature of ScalaTest which uses scala compiler at runtime as a library.
You can use typescript compiler as a library, it has rather complicated API documented here.
I have a node module published on github that simplifies things a bit, you can use it like this:
import {createCompiler, CompileResult} from 'tsc-simple';
const compiler= createCompiler({defaultLibLocation:'node_modules/typescript/lib'});
const r: CompileResult = compiler.compile('let x = 3 + 2');
assert.lengthOf(r.diagnostics, 0);
(using assert from chai module)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With