Comments (7)
I donāt think thatās the issue thatās being demonstrated here. Minimal repro:
const pNumber = Promise.resolve(0);
const pString: Promise<string> = pNumber.then(); // No error
The issue is that without a callback argument to then
, the only inference source for its type parameter is the contextual type (in my type annotation here, in the function return types in the repro from @blackdyedsheep).
from typescript.
This is the correct behavior - at runtime, .then(null)
creates a new Promise
with the original underlying value propagated out.
var a = Promise.resolve("hello world").then(null).then(x => console.log(x))
// prints 'hello world'
While you might prefer to never allow null | undefined
to be passed into .then()
, it's both allowed by the spec and permits patterns that are arguably useful (such as optionally-present handlers to transform values).
from typescript.
Whoops, I misread. Apologies.
from typescript.
The easy fix I thought of was to add an overload for optional undefined
/null
, but Iām not sure how well that stacks up against the easiest fix, ājust donāt write .then(null)
.ā Is there a real use case for writing code like that?
from typescript.
Yes, there is a real use case, that's how I came across this behaviour.
Quoting @DanielRosenwasser :
optionally-present handlers to transform values
ie this code should infer Promise<0 | string>
but infers Promise<string>
function maybeTransform(fn?: (v: number) => string) {
return Promise.resolve(0).then(fn);
}
and this code should not compile
function maybeTransform(fn?: (v: number) => string): Promise<string> {
return Promise.resolve(0).then(fn);
}
Basically it should behave the same way as
function maybeTransform(fn?: (v: number) => string) {
return fn ? fn(0) : 0;
}
from typescript.
I think this can be solved with overloads, but because overload resolution doesnāt happen during inference, I think itās going to be incredibly breaky. #58678 is up to experiment, but I donāt think it will be viable.
from typescript.
Probably nobody calls Promise.resolve(0).then<string>()
either except when they do. I'd say it's the same basic issue whether the type is inferred contextually from an unexpected place or the type argument is specified manually: generic defaults don't exactly match the use case, which looks like #56315. The use case in question is something like "when the optional thing is omitted by the caller, it is effectively supplied by the implementer". Generic defaults sort of do this, except when they don't.
from typescript.
Related Issues (20)
- Template Literal Type JSX children prop issue HOT 2
- [isolatedDeclarations] Add a syntactic form of computed property name which is always emitted as a computed property name HOT 5
- "Move to file" on global code unnecessarily imports/exports, generates invalid code HOT 3
- Wrong type on when using logical operators in nested conditions HOT 3
- Narrowing for constant indexed access fails on array getter HOT 3
- Design Meeting Notes, 6/7/2024 HOT 6
- Typescript 5.5 performance regression on arktype HOT 2
- Assertion violation in "move to new file"
- [nightly] Syntactically invalid declaration files emitted under extremely specific conditions HOT 1
- Make TypeScript Compilable to Native Machine Code! HOT 4
- [NewErrors] 5.6.0-dev.20240609 vs 5.4.5 HOT 58
- [ServerErrors][JavaScript] 5.6.0-dev.20240609 HOT 14
- [ServerErrors][TypeScript] 5.6.0-dev.20240609 HOT 24
- Special member completions not work after a comment.
- Module resolution: ESM-style namespace importing is broken HOT 5
- getSpecifierForModuleSymbol failing in 5.5 rc HOT 3
- Typescript process is consuming much memory and preventing suggestions at editor HOT 2
- Ignored tsconfig when targeting file HOT 2
- Control flow analysis for element access with variable index doesn't account for async race condition HOT 7
- URLSearchParams not accept number as a value HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
š Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ššš
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ā¤ļø Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.