|
Remember I wrote a simple configuration tool once open source on npm. It was written in javascript, because it's tooling. Not everyone writes Typescript backends, and once you've added it to your config you're done with it forever. Got hounded about adding typescript support for months, to the point where I said fine if you write a good typescript implementation I'll merge it. Then all of a sudden it was "oh i meant you should do it". After that I just said on the readme "don't ask about typescript" and I left the numerous issues up so that anyone could see this discussion had already happened. Typescript absolutists are annoying is what I'm getting at.
|
# ? Oct 19, 2022 21:18 |
|
|
# ? May 16, 2024 04:03 |
|
There one thing that's worse than JS and that's TS that is riddled with `any`s. I've been using TS almost exclusively for the last few years, after a decade+ with JS, and I will not start a new project of any serious complexity in JS these days. A nice type system is just so much easier to reason with, even if it's not perfect at this stage. I'd rather have the compiler warn me about me misremembering a function signature than see the issue pop up later during runtime just because JS was kinda able to run with it. Nolgthorn posted:Typescript absolutists are annoying is what I'm getting at. But this also. Just use the right tool for the job.
|
# ? Oct 19, 2022 21:36 |
|
I've been looking for reference libraries for handling scheduling, i.e. iCal format in an ECMAScript flavouring, apparently there is a recent update to the JSON version (RFC8984 ). There is a Google Apps Script API which looks nice enough front end too. So there are three main implementations, rrule.js, rSchedule, and dayspan. All three use TypeScript and dive into code golfing a bit too much. It's when the business logic of an implementation is 10 lines of code, but they push 1 million lines to a repo. It wouldn't be bad if the implementations worked, were maintained, had usable documentation. rrule.js randomly broke their repository, and somewhat impressively causes Chrome to fault so much it cannot show an error. rSchedule went to the template mall to satisfy problems no one has, like it "supports" 6 different DateTime implementations? They all have a long list of open bugs and missing functionality of the iCal spec. There is a lot of effort doing something with these projects.
|
# ? Oct 19, 2022 21:41 |
|
Nolgthorn posted:Remember I wrote a simple configuration tool once open source on npm. It was written in javascript, because it's tooling. Not everyone writes Typescript backends, and once you've added it to your config you're done with it forever. (Not that npm is exactly a joy to work with at the simplest of times.)
|
# ? Oct 20, 2022 01:37 |
|
So just making sure this is still correct as of now (since these things drift over time): there is no ironclad way to get a class name at runtime? I've noticed even without minifying constructor.name sometimes comes up weird and it seems like the conventional wisdom is not rely on it.
|
# ? Oct 21, 2022 03:52 |
|
I assumed variable names and class names are inherently anything and change because any number of different tools. If I needed a dictionary I'd build one explicitly.JavaScript code:
Then something like; JavaScript code:
JavaScript code:
JavaScript code:
Nolgthorn fucked around with this message at 13:09 on Oct 21, 2022 |
# ? Oct 21, 2022 12:21 |
|
https://jsfiddle.net/xmdsja3w/2/ The class itself has a name, the constructor is just a Function that gets created anonymously for the "class". If you minify the name will change, though!
|
# ? Oct 21, 2022 14:51 |
|
I get a typescript error saying name doesn't exist for an abstract class like thiscode:
I'm just playing around porting over my custom ecs from c# that uses a lot of generics for things like checking if an entity has a component X, gimme a list of all component X, gimme any entity that has X components, etc. The solutions in ts seem like awkward boilerplate at least for the various ts/js ecs setups I've seen on git.
|
# ? Oct 22, 2022 01:14 |
|
FuzzySlippers posted:I get a typescript error saying name doesn't exist for an abstract class like this But also that won't work because of the way Typescript does templates. When you do a template in C there is generated code for each specialization of T, so the function knows what T is while you're in it, but when you do a template in Typescript it's effectively just a loose constraint on what compiles to a single Javascript function, so there is no knowledge of what T is here for the javascript to retrieve a name from. Edit: so yeah doing this in js/ts is gonna be awkward boilerplate. roomforthetuna fucked around with this message at 02:08 on Oct 22, 2022 |
# ? Oct 22, 2022 02:06 |
|
roomforthetuna posted:I think you actually wanted T.name, because you're trying to get the name of the class not a name member from the object? I can't seem to do anything like that with just T. Just T seems to only work for casting. To get behavior like typeof(T) in c# I did find this a while ago code:
|
# ? Oct 22, 2022 03:52 |
|
FuzzySlippers posted:which I'm not entirely sure how it works as I pulled it off a random gist. The type declares an interface for a constructor and the class itself satisfies that and makes it survive the transition to runtime or some such? It seems to work pretty reliably, but I'm not sure if it actually is reliable and it is a bit more verbose. Think about what the javascript is that the typescript compiles into, essentially, to understand why some things work and some don't. All the type information is stripped away entirely, so casting becomes nothing, which is fine, but it can't compile to UnknownClassType.name or new UnknownClassType() which is why you can't do those things with a templated T-value. You can't even do if (obj instanceof T) because instanceof compiles to javascript, and T does not exist in javascript.
|
# ? Oct 22, 2022 13:44 |
|
My brain has stopped working, JS by reference, how does one access the updated value of a within the returned object?JavaScript code:
MrMoo fucked around with this message at 17:51 on Oct 24, 2022 |
# ? Oct 24, 2022 17:40 |
|
MrMoo posted:My brain has stopped working, JS by reference, how does one access the updated value of a within the returned object? The function 'b' returns the value of the closure variable 'a' after updating. The value 'a' in returned object will always return the value of 'a' at the moment the function 'f' was executed (it gets baked in at 'f()') To access updated a, you basically need to return a getter in your object, similar to 'b' just without the increment, which would be '{..., c: () => { return a; }}' At least it looks like that to me. e: this is a common technique to make variables 'private' in JS (by using closures) as JS doesn't have that facility built-in. e2: small tip, you don't need 'return' on arrow functions if you want to return the expression directly, in a one-liner, so 'b: () => ++a' and 'c: () => a' do the same thing as the above code gbut fucked around with this message at 18:07 on Oct 24, 2022 |
# ? Oct 24, 2022 18:02 |
|
A direct getter would be:JavaScript code:
It's weird to read as the syntax looks similar to normal closure, arrow function usage, but the result is different.
|
# ? Oct 24, 2022 18:22 |
|
MrMoo posted:We have private fields in whatever version of ECMAScript is current. Thanks for the correction. I knew private fields were in the works a while ago, but didn't check when they got adopted.
|
# ? Oct 24, 2022 19:22 |
|
Very new here. Working through a basic to do list to get my head around Javascript. The tutorial throws everything into an .html file. This works. If I separate files into .html, .css and .js, only one snippet does not work. I'm working in Visual Studio code. Chrome is up to date.code:
|
# ? Oct 24, 2022 23:11 |
|
ThePopeOfFun posted:Very new here. Working through a basic to do list to get my head around Javascript. The tutorial throws everything into an .html file. This works. If I separate files into .html, .css and .js, only one snippet does not work. I'm working in Visual Studio code. Chrome is up to date. Where in the HTML file is the script tag? If it’s before the <ul> tag the script won’t see it. If you open your browser developer console there may be an error logged.
|
# ? Oct 24, 2022 23:25 |
|
Bet that’s it. Script is at the top. Edit: Moving script to the bottom fixed it. Thanks. ThePopeOfFun fucked around with this message at 23:40 on Oct 24, 2022 |
# ? Oct 24, 2022 23:31 |
|
Best solution is to put all your code inside ofJavaScript code:
|
# ? Oct 25, 2022 01:17 |
|
Reminds me the awful Chrome team channel on YouTube, so much grandstanding and smugness all around. https://youtu.be/_iq1fPjeqMQ I think they're all on the Chrome 107 render blocking API now, as if that has any developer friendly usage (oh no, it's called DX "Developer eXperience"). https://developer.chrome.com/blog/new-in-chrome-107/#render-blocking-status MrMoo fucked around with this message at 17:05 on Oct 26, 2022 |
# ? Oct 25, 2022 01:31 |
|
Is there a way in Typescript to conveniently do something like named parameters, with defaults, where you also just keep the parameters in a single variable?code:
(In the real example there's a lot more than 3 values so each additional case of duplicating everything is significant pain, and positional variables, that do the effect more easily, make the code unreadable.)
|
# ? Nov 2, 2022 03:11 |
|
You could access the original object through the arguments array. But this doesn't help you - the way you're doing things, the default values only appear when you do the destructuring. Can I ask why you want to keep these as a single "params" object instead of using the parameters individually?
|
# ? Nov 2, 2022 04:05 |
|
Jabor posted:You could access the original object through the arguments array. But this doesn't help you - the way you're doing things, the default values only appear when you do the destructuring.
|
# ? Nov 2, 2022 13:15 |
|
roomforthetuna posted:Making them individual is one additional repetition right off the bat ("params: FartParams" versus "{a,b,c,d,e,f,g,h,i,j,k,l}: FartParams"), then if you want to store them in the class that took them in the constructor there's another repetition (you can't do {public a, public b} etc. so now you have to declare them all as members), and *another* repetition because you have to do this.a=a, this.b=b, etc in the constructor, which also means you're doing n operations in the constructor rather than just copying the reference. And then if you want to use these parameters (or a subset of them) in other functions or other classes you have to repeat them all again there too. If your destructuring the params object you’re _already_ doing n operations. One option to do what you want is to define a dict of defaults and merge them like this: code:
|
# ? Nov 2, 2022 15:20 |
|
necrotic posted:If your destructuring the params object you’re _already_ doing n operations. Mind, I'm not super concerned about the performance, it just sucks to be have code that's inefficient to write, inefficient to execute *and* not particularly readable either. Feels like if I have to make a lot of effort, it should be at least compile to code that runs better. But it seems the only option for performance is positional parameters, which makes all the non-performance considerations significantly worse. quote:One option to do what you want is to define a dict of defaults and merge them like this:
|
# ? Nov 2, 2022 23:22 |
|
roomforthetuna posted:True, but you're doing n operations *twice* then, when you destructure it *and* put it into member variables. I wouldn’t be concerned with the performance aspect of either approach at all. What that compiles to is going to be more efficient than you could do by manually unrolling (with destructuring and restructuring) and even that wouldn’t be something to be concerned with in almost all cases. Ergonomics is what matters more here.
|
# ? Nov 2, 2022 23:33 |
|
roomforthetuna posted:True, but you're doing n operations *twice* then, when you destructure it *and* put it into member variables. In your thinking about performance, have you considered the performance cost of doing an extra pointer indirection every time you look up what would have been a field?
|
# ? Nov 2, 2022 23:59 |
|
Jabor posted:In your thinking about performance, have you considered the performance cost of doing an extra pointer indirection every time you look up what would have been a field? You can reduce the indirection significantly if you're using multiple parameters in one function, like const fart = this.fart at the start of the function, then after that rfor the rest of the function it's one-hop indirection to the sub-params which you'd have had with the member variables anyway (this.smell vs fart.smell is the same). In Dart you can solve this sort of thing with chainable setters (fart..smell=a..visibility=b..taste=c), which you can kind of do in typescript by making setter functions that return 'this' (new Fart().setSmell(a).setVisibility(b).setTaste(c)), but, again, to do that you're requiring a shitload of boilerplate (and unlike the interface, that model also won't compile away to nothing). roomforthetuna fucked around with this message at 01:30 on Nov 3, 2022 |
# ? Nov 3, 2022 01:27 |
|
Can a real programmer explain why this doesn't work and what I should do instead?code:
The above code works but if I change "continuous" to false it still keeps running. Like it always thinks that "continuous" is true once it starts running. This is in a React app if that matters. e: never mind, I figured something out using useEffect. fuf fucked around with this message at 14:48 on Nov 9, 2022 |
# ? Nov 9, 2022 12:01 |
|
I'm new to react and I've been writing a class/component for a webpage but it's getting too long and hard to manage. I'd like to break the functions that create the HTML for the render function out into their own files, but I'm having trouble accomplishing this because the HTML in these newly external functions refer to functions in the class. Is there a way for an imported function to have access to the functions that already exist within the class? For instance, say the function that I'm importing returns an HTML drop down input, and when the input is changed I'd like it to call the handleChange function that already exists in the class, which would normally just be "this.handleChange.bind(this)". Can I write the HTML to call that handleChange function? Edit: This is close to what I want to do, but it still won't call the handleChange function, which is a function within the class. I just get this in the console: "Uncaught TypeError: Cannot read properties of undefined (reading 'handleChange')" https://stackoverflow.com/questions/47273663/storing-react-class-functions-in-separate-files Deadite fucked around with this message at 05:08 on Nov 14, 2022 |
# ? Nov 14, 2022 03:29 |
|
I might be misunderstanding because I'm new to React too but I think you just want to put your dropdown in a child component and call it from the parent. Then you can pass down any functions you want to the child. In the render method of your parent component you would have something like: code:
|
# ? Nov 14, 2022 10:22 |
|
Yep. I can't remember how to write class components (nobody uses them much any more, so if you are learning, unless you have a compelling reason to use them, don't) but basically:JavaScript code:
Lumpy fucked around with this message at 00:35 on Nov 15, 2022 |
# ? Nov 15, 2022 00:33 |
|
fuf posted:I might be misunderstanding because I'm new to React too but I think you just want to put your dropdown in a child component and call it from the parent. Then you can pass down any functions you want to the child. Thanks, I didn't know about parents/children and that seems to be what I was looking for. I put what I needed into props and now I can update the state the way I need to. Lumpy posted:Yep. I can't remember how to write class components (nobody uses them much any more, so if you are learning, unless you have a compelling reason to use them, don't) but basically: What does this mean? Should I not be doing the parent/child/props thing? I have no idea what I'm doing so if there's a better way I'd like to learn that.
|
# ? Nov 15, 2022 00:55 |
|
Deadite posted:What does this mean? Should I not be doing the parent/child/props thing? I have no idea what I'm doing so if there's a better way I'd like to learn that. Check out the beta docs/tutorials - they use function components instead of classes, which is the way (almost) all React code is written these days.
|
# ? Nov 15, 2022 01:19 |
|
Deadite posted:What does this mean? Should I not be doing the parent/child/props thing? I have no idea what I'm doing so if there's a better way I'd like to learn that. It just means there's two ways to write a React component, and the class component way is pretty much deprecated at this point. Class component with state TypeScript code:
TypeScript code:
teen phone cutie fucked around with this message at 01:27 on Nov 15, 2022 |
# ? Nov 15, 2022 01:23 |
|
There's nothing wrong with class components and they're still valid, but there's really no reason to write them anymore because it's just more code for no benefit.
|
# ? Nov 15, 2022 01:24 |
|
why does everyone write functions like this now:code:
code:
|
# ? Nov 15, 2022 17:15 |
|
Arrow functions look nicer to people who like functional programming, and there are different considerations regarding the binding of this, hoisting, etc. There's not that much difference in the end, mostly aesthetics.
|
# ? Nov 15, 2022 17:40 |
|
fuf posted:why does everyone write functions like this now: if you put HandleChange in a callback context, "this" will refer to the callback context with version 2. With version 1, "this" will be implicitly captured and refer to whatever it was when the function was defined.
|
# ? Nov 15, 2022 17:44 |
|
|
# ? May 16, 2024 04:03 |
|
fuf posted:why does everyone write functions like this now: Cargo cult worshiping, usually the same people believe https://standardjs.com/ is actually real. As in people actually refuse to use the function declaration now.
|
# ? Nov 15, 2022 22:24 |