Skip to content

Function with properties type inferred only at top level #31972

@danvk

Description

@danvk

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:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions