Skip to content

Problem with type intersection and typeof #33760

Closed

Description

TypeScript Version: 3.6.3

interface Type {
	obj: {
		a: number,
		b: number
	}[];
}

function f1() {
	return {
		obj: [
			{
				a: 1,
				b: 2,
				c: 3
			},
			{
				a: 11,
				b: 22,
				c: 33
			}
		]
	};
}

function f2() {
	const ret = f1();

	return ret as Type & typeof ret;
}

const t = f2();

function f3(obj: typeof t['obj'][number]): number {
	return obj.c;
}

const z = t.obj.map(o => f3(o));

I expect that the code works but the last line results in compilation error:

Argument of type '{ a: number; b: number; }' is not assignable to parameter of type '{ a: number; b: number; } & { a: number; b: number; c: number; }'.
  Property 'c' is missing in type '{ a: number; b: number; }' but required in type '{ a: number; b: number; c: number; }'.ts(2345)

The variable o implicitly gets the wrong { a: number; b: number; } type instead the correct { a: number; b: number; c: number; }.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions