Skip to content

Optional function argument type mismatch #54827

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
d07RiV opened this issue Jun 29, 2023 · 3 comments
Closed

Optional function argument type mismatch #54827

d07RiV opened this issue Jun 29, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@d07RiV
Copy link

d07RiV commented Jun 29, 2023

Bug Report

πŸ”Ž Search Terms

function optional argument parameter mismatch

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function foo(x: number, y: number = 1) {
    return x + y
}
type F1 = (x: number) => number
type F2 = (x: number, y?: string) => number

const f1: F1 = foo
const f2: F2 = f1

console.log(f2(1, 'a'))

πŸ™ Actual behavior

The code compiles without errors/warnings, despite an obvious type mismatch

πŸ™‚ Expected behavior

Should probably complain when narrowing function type from F1 to F2

@fatcerberus
Copy link

I think this is intentional, as it's equivalent to the (also intentional) type hole that exists with optional properties:

const foo = { x: 777, y: 1206 };
type O1 = { x: number };
type O2 = { x: number, y?: string };

const o1: O1 = foo;
const o2: O2 = o1;
console.log(o2.y);  // oops, not a string

@MartinJohns
Copy link
Contributor

Duplicate of #13043.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 29, 2023
@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

5 participants