Skip to content

Excess property check misses an error with union+string indexer #20060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sandersn opened this issue Nov 16, 2017 · 3 comments
Closed

Excess property check misses an error with union+string indexer #20060

sandersn opened this issue Nov 16, 2017 · 3 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@sandersn
Copy link
Member

sandersn commented Nov 16, 2017

export type Condition = And | Or | Not | Misc

export interface Misc {
	[type: string]: {
		target: string
		[key: string]: any
	}
}

export interface And {
	and: Condition[]
}

export interface Or {
	or: Condition[]
}

export interface Not {
	not: Condition
}

const c: Condition = {
        foo: "bar", // <-- should be producing an error
	not: {
		equals: {
			target: "name",
			value: "foo"
		},
	},
}

Expected behavior:

Error "foo is an excess property"

Actual behavior:

No error.

@sandersn sandersn changed the title Excess property check misses an error with union Excess property check misses an error with union+string indexer Nov 16, 2017
@sandersn
Copy link
Member Author

The excess property check is satisfied by Misc, which has a string indexer. No property will be marked as excess.

Structural assignability is satisfied by Not, since the object literal has the property not, but also an extra (excess!) property foo. But this is fine according to the rules of structural assignability.

The basic problem is that excess property checks happen before structural assignability finds the "best match" of the union; it uses a faster, heuristic check that doesn't perfectly match the exhaustive search used by structural assignability.

We could try moving excess property checking to the end of structuredTypeRelatedTo (or propertiesRelatedTo?) — object literals are guaranteed to reach this point, and they are the only things that are marked fresh right now. The code would be simpler because it wouldn't have to deal with unions. However, the performance gains from #16363 would very likely disappear.

@axiac
Copy link

axiac commented Feb 1, 2022

It seems that it had been solved in version 3.8.

@RyanCavanaugh
Copy link
Member

@axiac thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants