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

Excess properties not checked for unannotated function #33507

Closed
OliverJAsh opened this issue Sep 19, 2019 · 6 comments
Closed

Excess properties not checked for unannotated function #33507

OliverJAsh opened this issue Sep 19, 2019 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@OliverJAsh
Copy link
Contributor

TypeScript Version: 3.6.3.

Search Terms: excess property properties checks checking return type function annotation

Code

Are there plans to expand excess property checks so that this case is also covered?

type Fn = () => {
  foo: string;
};
const fn: Fn = () => ({
  foo: "foo",
  // No excess error :-(
  bar: "bar"
});

Workaround: annotate function return type or use constructor

type FnResult = {
  foo: string;
};
type Fn = () => FnResult;
const fn: Fn = (): FnResult => ({
  foo: "foo",
  // Excess error :-)
  bar: "bar"
});
@AnyhowStep
Copy link
Contributor

So, this is a feature request to propagate the "freshness" flag to the function return type, and perform excess property checks when the return type contains this "freshness"?

@AnyhowStep
Copy link
Contributor

Would you want this to give you an error?

function foo () {
  return { x : 1, y : 2 }
}
//OK?
//Or excess property error?
const obj : { x : number } = foo()

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Sep 19, 2019

How about,

type Fn = () => {
  foo: string;
};
function blah () {
  return {
    foo: "foo",
    bar: "bar"
  }
}

//OK?
//Or excess property error?
const fn: Fn = () => blah();

//OK?
//Or excess property error?
const fn2: Fn = blah;

@AnyhowStep
Copy link
Contributor

AnyhowStep commented Sep 19, 2019

How about,

type Fn = () => { foo : string }[];

const fn : Fn = () => ["hello"].map(str => {
  return {
    foo : str,
    //OK?
    //Or excess property error?
    bar : str,
  };
});

Or,

type Fn = () => { foo : string }[];

//Does this freshness propagate through generics?
function identity<T> (callback : () => T) : T {
  return callback();
}

//OK?
//Or excess property error?
const fn : Fn = () => identity(() => ({
  foo: "foo",
  //Excess property
  bar: "bar"
}));

//Does this freshness propagate through generics?
function identity2<T> (t : T) : T {
  return t;
}

//OK?
//Or excess property error?
const fn2 : Fn = () => identity2({
  foo: "foo",
  //Excess property
  bar: "bar"
});

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 19, 2019
@RyanCavanaugh RyanCavanaugh changed the title Excess properties not checked for annotated function Excess properties not checked for unannotated function Sep 19, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #12632, lots of others.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

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

4 participants