Open temeddix opened 9 months ago
I agree on it doing too much, but that doesn't make what you propose not also too much. But hey, I proposed no syntax change to JS (https://github.com/tc39/proposal-type-annotations/issues/176), so everything will look too much compared to it.
Anyway, if there is a syntax change, why should it be in the form of TS/C#/Java? Just because that's what people are used to? What you are used to?
The way I proposed it - using comments, it's extensible with TS syntax (as long as you don't polute the actual JS code, but put your type
and interface
in a comment), but it also doesn't hamper some other kinds of extensions, other ways of type system notation.
removing interfaces and common typed-array syntax would be disappointing for me
Hi, please let me share my opinions about this proposal, because this proposal seemed to get more and more overwhelming. I know I'm not in any position to direct things, but after many months of community debate, I wanted to suggest a new point of view.
I am not sure if the proposal is heading the right direction. To me, it looks like the whole discussion is influenced too much by the way TypeScript does types, including things that are not really necessary.
To do and not to do
The philosophies of JavaScript and TypeScript about handling types are different. JavaScript uses simple prototype-based class system, while TypeScript uses
interface
,type
, andclass
.The reason for 3 different syntaxes for declaring types in TypeScript is known to be a historical path of TypeScript, and there have been a lot of controversies around TypeScript community for many years(about when to use what). I would argue that this system is not ideal, and it has a lot of technical debt.
What if we stick to the current prototype-based class system in JavaScript, only with the new 'type annotation'? This would be similar to Python's 'erasable' type hint system. This way, the script size doesn't get too bigger than now, but type safety is achieved.
class
number?
, like Dart)type
statement (Maybe only as an alias)interface
namespace
(JavaScript has a module system)::
(JavaScript is not a compiled language)Dealing with JSON
Then we come up with a question.
Yes, it is a common practice to use
interface
andtype
statements in TypeScript to structure the JSON schema nowadays. However, it can definitly be done with the existingclass
system of JavaScript without the complexity of TypeScript.Take a look at this Rust example:
Rust elegantly achieves nested JSON schema with type-safety like above. In fact, there's a JavaScript library dedicated to achieve the same thing:
This library contends that we should use classes for type-safe JSON schemas.
Currently,
class-transformer
library requires a bit of boilerplate code like@Type(() => Photo)
, and I think this proposal can include a new method likeObject.getTypeAnnoationOf(MyClass, 'fieldName')
to support typed serialization/deserialization of nested JSON. Maybe we can provide an optional type argument for JSON functions, like this:Array type representation
The TypeScript team somehow decided that an array of some type is represented as
MyClass[]
, notArray<MyClass>
(though both work).MyClass[]
syntax is used in low-level languages such as C, C++, and Rust, where an array is actually a continous memory space. However, this does not really make sense for high-level languages like JavaScript. PerhapsArray<T>
should be explicitly required as a language spec.Array<MyClass>
MyClass[]