Giter VIP home page Giter VIP logo

Comments (7)

andrewbranch avatar andrewbranch commented on June 22, 2024 1

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.

DanielRosenwasser avatar DanielRosenwasser commented on June 22, 2024

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.

DanielRosenwasser avatar DanielRosenwasser commented on June 22, 2024

Whoops, I misread. Apologies.

from typescript.

andrewbranch avatar andrewbranch commented on June 22, 2024

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.

blackdyedsheep avatar blackdyedsheep commented on June 22, 2024

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.

andrewbranch avatar andrewbranch commented on June 22, 2024

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.

jcalz avatar jcalz commented on June 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.