Class-based Tree Shaking - HTTP203

preview_player
Показать описание
Jake Archibald and Surma talk tree shaking in this episode of HTTP203. And show you some code! For the first time. Lovely.

Рекомендации по теме
Комментарии
Автор

It absoluteley is possible for typescript, here you go:


type ParamsOf<T extends Function> = T extends (...args: infer P) => any
? P
: never
type ReturnOf<T extends Function> = T extends (...args: any[]) => infer R
? R
: never

class DoClass {
do<T extends Function>(func: T, ...args: ParamsOf<T>): ReturnOf<T> {
return func.call(this, ...args)
}
}

const x = new DoClass()
function hallo(a: string, b: number, c: Error) {}
x.do(hallo, '', 2, new Error())

MrDomSan
Автор

As of TypeScript 3.0, it is now possible to do what you want using the new and improved tuple features (albeit without JSDoc and autocomplete).

nhardy
Автор

Great video! Certainly a great improvement to show code :)

avilde
Автор

The Store.do method is now possible in TypeScript!

clo
Автор

Yes, thanks for listening to the feedback, great video!

jackninja
Автор

Well, yes, the code samples are a great addition, but where can we get the Downasaur and awkward family portrait coasters? 🤔

ShaneLawrence
Автор

what's ugly about closures and curried functions? @8:15

BennyPowers
Автор

Partial application from the first version of |> is great but when would someone choose to use the second version to write a |> b(?, c) instead of just b(a, c)?

wrburdick
Автор

Thanks for code examples. A real nice addition to your series.

FrederikDussault
Автор

You can do that in TypeScript now.

function do<A extends any[], R>(doFn: (...args: A) => R, ...args: A): R { return doFn(...args); }

Hector-bjls
Автор

Bit late to the party. But how about

import { get, set, Store } from './idb-keyval.js';
const store = Object.assign(new Store('my-store'), { get, set });

CalvinMannedUp
Автор

I wonder if we couldn't get the static analyser to detect if any square bracket operators are used on the Store object. If there are, the Store is marked as a dirty object and can't be tree shaken. If there aren't then it can be tree shaken.

This would probably solve 90% of the tree shake issues. And the remaining 10% can use the more obscure solutions you mentioned in the videos.

I quess it would still be an issue for certain data type classes. For instance a List class that has a shift method, but also an index operator to get a specific item. Although you could replace the List[index] with List.get(index)

BertVerhelst
Автор

@10:40 TypeScript: "It's definitely more complicated than it should be" 🤣

BennyPowers
Автор

That pipe operator is a beautiful beautiful thing in Elm and languages alike. It also goes hand-in-hand with partial application which is kinda covered in JS with `bind`. Trust me fellas, you would very much enjoy it if you practice even a little bit of functional style transformations.

DanielPopeski
Автор

The do() method makes "static" calls into "runtime" calls, it makes call stack unnecessarily polluted, and render autocomplete and jsdocs useless...

victornpb
Автор

yes, it is very understandable than before, please include code example in all sessions.

thequizclassroom
Автор

One of the many reasons why I like Haxe over TypeScript.
Haxe has had Static Extensions since forever. (the "using" keyword)

mpcref
Автор

You guys are awesome ! How can we buy those coasters ?

firstprincipleslearning
Автор

I ran into this very problem in isomorphic-git. At one point I had a constructor `let repo = new Git({fs, dir})` and instance methods... but after realizing some of the methods added _serious_ weight (100kb?!) I was determined to allow users to only import the methods they used. Ultimately I ended up with an API where the state management is external to the library. Instead of providing a constructor, I just provide all the operations as functions and make the user pass certain required arguments that I would have stored on "this" every call. `let result = fetch({fs, dir, ...args})`

wmhilton-old
Автор

I don't see why can't the set function receive 3 arguments... it's easier to grasp than almost all the alternatives.

Other than that, partial application is a beautiful thing once you get the hang of it! Right now it's not the more performant way but I'm sure the engines will optimize them in no time!

feldinho