4 ms·
Honestly typescript is fine, just give me: Standard library that isn't worthless - no more npm package spam. Type knowledge in the runtime. It doesn't have t
by bioemerl 3y ago
Honestly typescript is fine, just give me:
Standard library that isn't worthless - no more npm package spam.
Type knowledge in the runtime. It doesn't have to be typed, just make it so I don't have to do crazy checks on properties before casting to a class.
Imports without webpack.
- crgwbr 3y agoGive io-ts a try for the runtime type checking. We’ve used it in prod for a few years now and it’s pretty solid.
- amitport 3y ago> "just make it so I don't have to do crazy checks on properties before casting to a class." You do have 'instanceof' at runtime, so unless you mean some other type of 'class', casting to a class is really a non-issue (and you don't really 'cast'... since dynamic typing and all). If you mean some more advanced concept (e.g., structural type runtime checking), think how you would do it in statically typed languages (is it really better?).
- bioemerl 3y agoInstance of doesn't work sometimes and you have to write explicit property checks. I forget the details because I'm by no means a typescript expert, but I remember being very frustrated by the fact I couldn't just use instance of.
- masklinn 3y agoIt doesn't work when objects cross global scope boundary (e.g. you get an object from a frame), there's no fixing that, by design the namespaces are different and unrelated.
- deleted 3y ago[deleted]
- lacerrr 3y agoinstanceof only works with JavaScript classes. Not on regular objects that just structurally conform to a type or interface.
- deleted 3y ago[deleted]
- likeclockwork 3y agoClasses aren't the only way to define types?
- amitport 3y agoStructural typing is a thing.. also there aren't really classes in js in the Java sense, it's all prototype chains.
- likeclockwork 3y agoinstanceof only works for inherited types, usually expressed with the class keyword by TypeScript users. It's entirely worthless if you're not using them. Class is the only construct in TS that is both a type and a value.
- randomdata 3y agoJavaScript has half decent object construction syntax that avoids the nesting hell of classes: function Foo(s) { this.s = s } Foo.prototype.bar = function() { console.log(this.s) } const baz = new Foo("Baz") baz.bar() But it seems Typescript can't handle it?
- amitport 3y agoWell there are 2 issues: (1) imperative style mutation of the prototype is runtime dependent, and typescript can't really rely on runtime behavior. (2) Foo has two incompatible types in JS, it is both `(s) => void` and `new (s) => Foo`, typescript by default will assume the first signature (which is usually what people want). In any case, you can specify the types (though in 99% of the cases, just use a class): interface Foo { s: string; bar(): void; } const Foo = function(this: Foo, s: string) { this.s = s; } as { new (s: string): Foo; (this: Foo, s: string): void; }; Foo.prototype.bar = function(this: Foo) { console.log(this.s); } const baz = new Foo('Hello'); baz.bar()
- 3y ago
- mcphage 3y ago> Standard library that isn't worthless This is exactly what bugs me about TypeScript. The language itself is fine—some annoying things, but generally they’re due to having to run on top of JS. But Microsoft had the chance to fix JS’s terrible standard library. And they didn’t!
- masklinn 3y ago> Imports without webpack. https://caniuse.com/?search=import https://caniuse.com/?search=import
- bioemerl 3y agoImports through the browser do, but I'm wanting a single file bundle with all those imports so I'm not relying on a browser feature to deliver the content over N requests. I know that's less of a concern nowadays with modern http, but guess who still supports ie11
- WorldMaker 3y agoI'm sorry you are stuck still supporting IE11. You could use an AMD loader and use imports in Typescript configured to transpile AMD modules. Most AMD loaders were tiny. The benefit to AMD modules in your case is that they don't need webpack to bundle, the (hideous) module format was designed to be bundled by raw concatenation and the Typescript compiler will still even do that for you with the --outFile parameter (or "outFile" tsconfig.json setting). It feels like terrible advice to give someone, given ESM is far superior to AMD, and this would be going backwards in time, but in your case you are already blighted by the disease and it is advice that might staunch some of the bleeding.
- IceSentry 3y agoWhat's missing in the standard library though? I hear this complain often, but when I use a library it's always for a specific use case and not something that I would expect to be in a standard library.