Skip to content
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

Error on extra parameters in return value #5495

Closed
chadaustin opened this issue Nov 2, 2015 · 3 comments
Closed

Error on extra parameters in return value #5495

chadaustin opened this issue Nov 2, 2015 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@chadaustin
Copy link

Just as in #3755, I would expect extra parameters from return values to produce an error.

On the playground:

interface P<T> {
    fields: T;
}

function f(): P<{}> {
    return {
        fields: {
            x: 1,
        }
    };
}

function g(): {} {
    return {
        y: 2,
    };
}

function h({one}: {one: string}) {
}

h({one: "hi", two: "weeeee"});

Neither f nor g produce an error. Calling h does, per #3755.

I would expect f and g to produce errors too.

@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Nov 2, 2015
@RyanCavanaugh
Copy link
Member

The empty type does not trigger warning about extra properties. This is intentional behavior -- it doesn't make sense to error when there is no valid property name you could write.

Note that if f or g had some properties, you would see an error.

function f(): P<{y?: string}> {
    return {
        fields: { // Error here
            x: 1,
        }
    };
}

@chadaustin
Copy link
Author

Fair enough that it's intentional, but in my particular case, it would be useful to have an error when returning fields not in {}. Consider:

interface CoreData {
    name: string
}

interface Response<ExtraData> {
    core_data: CoreData
    extra_data?: ExtraData
}

function foo(): Response<{age: number}> {
    return {
        core_data: { name: "laphroaig" },
        extra_data: { age: 18 },
    }
}

function bar(): Response<{}> {
    return {
        core_data: { name: "laphroaig" },
        extra_data: { age: 18 }, // woops! should be no extra data here
    }
}

I want bar to be an error, because I didn't meant to include any extra fields. Is there a way to express this rule in TypeScript? Perhaps something other than {} ?

@RyanCavanaugh
Copy link
Member

You could write something like this:

// Use any suitably-unlikely thing to write. Maybe 'ϞϞ(๑⚈ ․̫ ⚈๑)'
type EmptyType = { 'I really meant empty'?: void }`;
function bar(): Response<EmptyType> {
 /*... works as you'd like now ... */
}

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants