microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
99k stars 12.29k forks source link

Design Meeting Notes, 6/7/2024 #58804

Open DanielRosenwasser opened 3 weeks ago

DanielRosenwasser commented 3 weeks ago

Control Flow over Callback Arguments, and the Deferred Marker Type

https://github.com/microsoft/TypeScript/pull/58729

fatcerberus commented 3 weeks ago

Deferred helper type

Shouldn’t it be the other way around - a NonDeferred helper type? If you treat “non-deferred” as the default then you end up causing the same breaks you were trying to avoid (code that assumes a callback is deferred and has its narrowings cancelled prematurely)

plus I’d think that functions calling a callback immediately are probably in the minority relative to ones where it’s deferred/called asynchronously.

fatcerberus commented 3 weeks ago

plus I’d think that functions calling a callback immediately are probably in the minority relative to ones where it’s deferred/called asynchronously.

…unless someone just got finished reading a monad tutorial and went mad with power after it finally clicked for them 🚎

DanielRosenwasser commented 3 weeks ago

The idea is that this would be behind a --strict flag which takes a more conservative view of narrowing - and declaration sites would be able to opt out of that.

fatcerberus commented 3 weeks ago

Hmm… it seems like it’s probably pretty idiomatic today to do stuff like

let foo: Foo | null = createFoo();
foo.onFinished(() => {
    foo = null;
});
// now foo is fully initialized, pass it around

As a matter of principle I’d feel uncomfortable passing foo around before all its callbacks were registered, and it wouldn’t be feasible for me to turn this proposed flag on until the library writer updated their types to align, which might make it too breaky even for strict

My point stands about the potential for code bloat with Deferred vs. NonDeferred too

Josh-Cena commented 3 weeks ago

plus I’d think that functions calling a callback immediately are probably in the minority relative to ones where it’s deferred/called asynchronously.

I would postulate that most callbacks are array method callbacks, FWIW.

DanielRosenwasser commented 3 weeks ago

and it wouldn’t be feasible for me to turn this proposed flag on until the library writer updated their types to align, which might make it too breaky even for strict

This is a bit of my concern too - that people would try this out, find it's too painful, and forget to try to turn it on again in the future.