Description
The Awaited
Type in lib.d.ts
-
When we introduced
await
into TypeScript, we added some logic to recursively unwrap a type. -
At the time, we had no recursion mechanism within the type system.
-
First we started out with an
awaited
type operator. -
Problem: if you have generics, you end up with higher-order type operators.
async function f<T>(x: T) { await x; // awaited T }
- This is technically correct, but very cumbersome.
- The problem is that
awaited T
is not necessarily assignable toT
(nor vice versa).
-
Had ideas for a top-level type alias...
-
And that's what we do here, but right now are only introducing the alias to be used in
lib.d.ts
.- Behavior is based on what the
await
operator does.
- Behavior is based on what the
-
Preserves the incorrect behavior to avoid errors on
awaited T -> T
in async functions, but better-modelslib.d.ts
. -
Ideally, other overloads take care of the breaks.
-
Does this break existing code?
- Probably, but question is how much.
- Have to run our tests.
-
Having
Awaited
makes sense in general. -
Worth thinking about whether users will be able to understand how to use this.
-
Should we get rid of the other overloads?
- Want to try to!
-
Conclusions
- Need to see what this breaks.
- Need to see if we can remove overloads.
- Maybe see if this can be leveraged by
await
?
--noErasingImportedNames
- Current behavior is that if you write an import for something, if you don't use it you lose it.
- [[How did nobody think of phrasing it that way until now?]]
- Under this mode, every imported name is preserved if it has a value meaning.
- Under
isolatedModules
, it will warn you that some compiler won't know how to drop types.- Then the name is a misnomer. It's about dropping values.
- Great, we can bikeshed this.
- Who would use this?
- Useful for
-
Svelte.
<script> import Layout from "./Layout.svelte"; </script> <Layout />
- TypeScript doesn't see what the transformer does, needs to not drop
Layout
.
- TypeScript doesn't see what the transformer does, needs to not drop
-
Vue
- Not vanilla Vue SFCs.
<script setup>
. - Similar to Svelte use-case - top-level declarations are implicitly brought into the template code.
- Not vanilla Vue SFCs.
-
noErasingImportedNames
hard to parse.preserveValueImports
"importsNotUsedAsValues": "preserve"
😢
Stack Overflow on Type Instantiation
- We increased the type instantiation limit from 50 to 500.
- How do we know we're about to blow the stack?
- Nothing we can do?
- Least common denominator?
- Changing target, could be dynamic on the browser itself.
- Can try to guard against these with a
try
/catch
.try
/catch
at the top ofinstantiateType
?- Could, but how do you guard against inconsistent state?
- Could pre-emptively blow the stack and create a guesstimate from that.
- [[Think my idea just got laughed down. 😅]]