GithubHelp home page GithubHelp logo

Comments (5)

ahejlsberg avatar ahejlsberg commented on September 25, 2024 1

Yeah, there's an issue here. This is a simplified repro:

type Foo = { kind: "a" | "b", value: number } | { kind: "a", value: undefined } | { kind: "b", value: undefined };

function test(obj: { kind: "a" | "b", value: number | undefined }) {
	let x1: Foo = obj;  // Ok
	let x2: Foo = { kind: obj.kind, value: obj.value };  // Error, but shouldn't be
}

Above, our type discrimination logic reduces type Foo to just { kind: "a" | "b", value: number } because it is the only constituent of the union to which the type of obj.kind is assignable. This unidirectional assignability check is a little too selective. We should really be checking whether there is any overlap between the discriminant type in the object literal and the discriminant type in the target--in other words, the "a" | "b" type from the object literal shouldn't eliminate the "a" and "b" choices in the target. It's a pretty simple fix. I'll put up a PR.

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on September 25, 2024

Why isn't TypeB written this way?

type TypeB = {
	kind: MyEnum.B;
} & ({ id?: number | undefined; });

from typescript.

WGroenestein avatar WGroenestein commented on September 25, 2024

Why isn't TypeB written this way?

type TypeB = {
	kind: MyEnum.B;
} & ({ id?: number | undefined; });

As mentioned above, in the real world example were we encounter this, the "id: number" type branch can have additional fields. While when not providing an id (or undefined), you should not provide these. E.g.

type TypeB = {
	kind: MyEnum.B;
}
& (
	{
		id?: undefined;
	}
	|
	{
		id: number;
		commentId? number;
	}
);

from typescript.

fatcerberus avatar fatcerberus commented on September 25, 2024

You can’t actually prevent a commentId from appearing in the id?: undefined case without exact types - see #12936

from typescript.

RyanCavanaugh avatar RyanCavanaugh commented on September 25, 2024

Bisects to #53709

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.