Skip to content

Function property assignment names conflict with function return value #33741

Closed
@bradleyayers

Description

@bradleyayers

An error occurs when adding a property to a function that conflicts with the name of a property of the return type of the function. For example if the function returns an array, an error is reported when trying to assign any properties that have the same name as array prototype properties.

TypeScript Version: 3.6.3 (works correctly in 3.5.3)

Search Terms:

  • function property assignment return value conflict

Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
function foo(): string[] {
  return [];
}
// Assigning to `join` erroneously conflicts with `Array.prototype.join`
foo.join = "";

Expected behavior:

Any property name should be allowed.

Actual behavior:

Type '""' is not assignable to type '(separator?: string | undefined) => string'.

Property names conflict with the names of property on the return type. In this case the return type is string[], which has a join property, and the type checker is treating these two as the same namespace.

Playground Link: https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABMOcAUBKAXIgzlAJxjAHMBtAXUQG8AoRRAgUyhAKUoG5aBfWlOADoAVnGKIAvIgBE0zkA

It looks like a test should be added to https://github.com/microsoft/TypeScript/blob/master/tests/baselines/reference/typeFromPropertyAssignment29.js.

Related Issues:

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions