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

Confusing rules around function parameter names in a type #53933

Closed
phillipb opened this issue Apr 20, 2023 · 4 comments · Fixed by #53946
Closed

Confusing rules around function parameter names in a type #53933

phillipb opened this issue Apr 20, 2023 · 4 comments · Fixed by #53946
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@phillipb
Copy link
Member

phillipb commented Apr 20, 2023

Bug Report

In auditing some code, I've noticed something that doesn't seem like it should compile. In >=4.95, the following code compiles without an issue, but in <=4.7.4, there is an error. Why is it possible to specify an object for the name of a parameter? This code was likely the result of a bad copy/paste, but TS isn't erroring on it. Am I missing something about the syntax? Also, is the discrepancy between versions intended?

const DEFAULT_NUM_PAGINATED_RESULTS = 10;
type IPaginatedParams = {first?: number; after?: string;}

type GetPaginatedResults = <Obj, Key extends keyof Obj>(

  items: Obj[],
  
  { first = DEFAULT_NUM_PAGINATED_RESULTS, after }: IPaginatedParams,
  
  key: Key
  
) => { items: Obj[]; hasMore: boolean };

Playground links for 4.7.4 vs 4.9.5

🔎 Search Terms

function parameter names

@typescript-bot
Copy link
Collaborator

The change between origin/release-4.8 and origin/release-5.0 occurred at 3b45f4d.

@fatcerberus
Copy link

fatcerberus commented Apr 20, 2023

That looks like… a valid destructuring, I think? The function takes an IPaginatedParams object as its second argument and then destructures it, extracting the first and after properties from it. It is kind of weird to do that in a function type though, since there’s no function body to use the extracted properties—all that matters there is the IPaginatedParams annotation.

@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Apr 20, 2023

Looks like an unintended consequence. Bisects to #49089

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 20, 2023
@RyanCavanaugh
Copy link
Member

Minimal:

// Should syntax error
type Foo = ({ first = 0 }: { first?: number }) => unknown;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants