Skip to content

Typing on function behaves differently if as type, interface or on the function #43365

Closed
@mmahalwy

Description

@mmahalwy

Bug Report

No error:

type CustomType = { name: string };
interface MyFunc {
  (arg: string): CustomType;
}

const myFunc: MyFunc = (arg) => {
  return { name: arg, extraKey: 1234 };
};

No error:

type CustomType = { name: string }
type MyFunc = (arg: string) => CustomType;

const myFunc: MyFunc = (arg) => {
  return { name: arg, extraKey: 1234 };
};

Errors:

type CustomType = { name: string }

const myFunc = (arg: string): CustomType => {
  return { name: arg, extraKey: 1234 };
};

// error: Type '{ name: string; extraKey: number; }' is not assignable to type 'CustomType'.
//  Object literal may only specify known properties, and 'extraKey' does not exist in type 'CustomType'.

🔎 Search Terms

Function types not erroring when using type or interface.

🕗 Version & Regression Information

Version 4.2.3

Tested from version 3.3.3 to 4.2.3

⏯ Playground Link

N/A

Playground link with relevant code

💻 Code

See above

🙁 Actual behavior

Only the last example fails.

🙂 Expected behavior

All examples above should error. The function returns an object with incorrect keys.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions