Open spillz opened 1 year ago
Re assertions, VS Code does a nice job of flagging invalid ones at least, which can save some refactor bugs:
Also, the VS Code ergonomics for type annotating JSDoc's for existing code are pretty decent too. Just tab/type
And provides completion hints etc. just like TS:
yeah, well... the language service provider for jsdoc (and javascript in general) in vscode is typescript, after all
There is a common misconception that there is an intrinsic need for an extra pair of parenthesis
@lillallol according to that issue you linked they are required. That's all I was saying.
The abstract class gets compiled to an empty class. I am just trying to understand why would anyone extend an empty class, so that I can make a correct conversion?
@lillallol
@trusktr
Regarding the example you have posted link it all boils down to separating the implementation and the type of an abstract class. Unfortunately TypeScript does not provide such a feature (yet). It is my understanding that this a matter of support and not something that intrinsically can not be supported. Here is how I would do it if such a feature was supported:
test.js
export class Bar {bar = 123};
/**@type {import("./index").IFoo}*/
export const Foo = class {
//This method will not be needed if TypeScript support abstract member in
// IFoo
method() {
throw Error();
}
realMethod() {
const ret = this.method()
console.log('do something with subclass value', ret)
return ret
}
}
/**@type {import("./index").IFooBar}*/
const FooBar = Foo;
class Test extends FooBar {
method() { return new Bar() } // ok
}
class Test2 extends FooBar {
method() { return new Object() } // type error
}
new FooBar() // type error
const b = new Test().realMethod();
// does not type error because abstract member in IFoo is not supported yet by
// TypeScript, although it will throw error in runtime
(new Test()).method()
./index.ts
import { Bar, Foo } from "./test";
export type IFoo = abstract new <T>() => {
//It all boils down to TypeScript supporting abstract members
abstract method() : T
realMethod() : T
};
export type IFooBar = typeof Foo<Bar>;
The title is dramatic but it is at least a concise expression of how I feel about a proposal that adds an enormous amount of syntactic complexity and expressive overhead to the language with what is intended to be no runtime implications. I too value the benefits that type annotations can provide but I believe the annotation approach in this proposal suffers from a few major flaws relative to simpler annotation approaches:
Given the above, I don't believe the alternative of a JSDoc-ish syntax that would cleanly delineate annotations from functional code is being taken seriously enough in the proposal. By "JSDoc-ish" I don't mean that the types should literally be embedded in comments but, for example, a type annotation marker (for single lines perhaps "@", "#" or "~") could be added to the language that is functionally equivalent and as easy to parse as a comment at runtime but also easily distinguishable from an actual comment. That at least means you could not interweave annotations and code on the same line as proposed without actually opening and closing the annotation (e.g., /@, @/) or restricting annotations to the end of a line (a la // comments). My preference would be for annotations to always be on separate lines (or separate blocks of lines to longer defs) from functional code each demarked by a single leading character, which would then make them extremely easy to strip at runtime and easily distinguished by developers. The type annotation syntax itself should still be part of the ES spec, which I believe would be important for adoption and to avoid fragmentation. A motivation to include the syntax in the spec is that the annotations could also be used for inference inside of browser debugging tools. I've seen examples of one liner annotations prefixing lines of functional code such as methods and functions given in other Issues here that look as readable as the TypeScript version even without IDE UI hints, especially if you replaced the comment marker with another character.
If you got this far, thanks for considering an alternative viewpoint!