Skip to content

Function with properties type inferred only at top level #31972

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
danvk opened this issue Jun 18, 2019 · 5 comments · Fixed by #38031
Closed

Function with properties type inferred only at top level #31972

danvk opened this issue Jun 18, 2019 · 5 comments · Fixed by #38031
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@danvk
Copy link
Contributor

danvk commented Jun 18, 2019

TypeScript Version: 3.5.1

Search Terms:

  • 2739
  • function properties block
  • top level function

Code

interface Person {
  first: string;
  last: string;
}

const dice = () => Math.floor(Math.random() * 6);
dice.first = 'Rando';
dice.last = 'Calrissian';
const diceP: Person = dice;  // OK

// but inside a block...
{
  const dice = () => Math.floor(Math.random() * 6);
  dice.first = 'Rando';  // Property 'first' does not exist on type '() => number'. 
  dice.last = 'Calrissian';  // Property 'last' does not exist on type '() => number'.
  const diceP: Person = dice;  // Type '() => number' is missing the following properties from type 'Person': first, last
}

Expected behavior:

I would expect the same behavior both inside and outside the block.

Actual behavior:

Assigning properties to a function at the top level is allowed, but not inside a block. Frankly, I'm impressed that the first example works at all. But I don't see why they would be different!

Playground Link: link

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jun 25, 2019
@RyanCavanaugh
Copy link
Member

This is the intended behavior - you can "declare" new properties "locally" with bare assignments, but you can't just tack on new properties anywhere

@typescript-bot
Copy link
Collaborator

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

@weswigham weswigham added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Jun 27, 2019
@weswigham
Copy link
Member

Second look: Probably a bug. Moving the same code into a block shouldn't really affect it's validity, as is shown here.

@weswigham weswigham reopened this Jun 27, 2019
@weswigham weswigham added this to the TypeScript 3.6.0 milestone Jun 27, 2019
@weswigham
Copy link
Member

weswigham commented Jun 27, 2019

cc @sandersn there's some sort of check we have for top-level blocks, but not nested blocks, which seems quite unfortunate here.

@sandersn
Copy link
Member

Check the binder in bindPropertyAssignment; it's where the most complex top-level check is, and is probably the culprit. I don't think there are any top-level checks in the checker, but I could be wrong.

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.

7 participants