by com2kid · 2y ago · 0 comments
It'd be nice of TS at least allowed for specifying types like integer, allowing some of the newer TS aware runtimes could take advantage of the additional info, even if the main TS->JS compilation just treated `const val: int` the same as `const val: number`.
I wonder if a syntax like
const counter: Number<int>
would be acceptable. const Integer = {
[Symbol.customMatcher]: (value) => [Number.parseInt(value)]
}
const Integer(counter) = 42.56;
// counter === 42
[1] https://github.com/tc39/proposal-extractors const counter = UInt64(42);
The current state of the art is const counter = BigInt.asUintN(64, 42);Because JS code can arbitrarily modify a type, any language trying to specify what the outputs of a function can be also has to be Turing complete.
There are of course still plenty of types that TS doesn't bother trying to model, but it does try to cover even funny cases like field names going from kebab-case to camelCase.