Skip to content

Conditional keys added to Partial<T> are not checked #38896

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
pierpo opened this issue Jun 2, 2020 · 2 comments
Closed

Conditional keys added to Partial<T> are not checked #38896

pierpo opened this issue Jun 2, 2020 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@pierpo
Copy link

pierpo commented Jun 2, 2020

Search Terms:

conditional keys object properties not checked

Code

interface MyInterface {
    a: number;
    b: number;
}

const myFunc = (): Partial<MyInterface> => {
    return {
        a: 1,
        b: 2,
        ...(true && { c: 'oops, no error' }),
    }
}

Expected behavior:

I should have an error saying "c key does not exist in MyInterface".

Actual behavior:

I do not get an error.

Playground Link:

Playground Link

Related Issues:

I could not find any.


Please note that the values are checked. This does produce an error:

interface MyInterface {
    a: number;
    b: number;
}

const myFunc = (): Partial<MyInterface> => {
    return {
        a: 1,
        ...(true && { b: 'okay I get an error' }),
    }
}
@MartinJohns
Copy link
Contributor

Excess property checks do not occur for spread objects. This is intentional, see #19775.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 2, 2020
@pierpo
Copy link
Author

pierpo commented Jun 2, 2020

Oh! Indeed. I did not pinpoint the problem precisely enough, I should've known it was related to spread operators.

Thank you for your very quick and helpful answer! We may close the issue! Sorry about the duplicate 😊

@pierpo pierpo closed this as completed Jun 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants