microsoft / microsoft/TypeScript
Prototype assignment should take the type of the initializer and not require a literal
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
This is a feature request based on actual VS customer code.
TypeScript Version: 3.9.2
Search Terms: ES5 class javascript prototype assignment constructor function
Expected behavior: In the example below, the prototype for test.class should include properties from testPrototype.
Actual behavior: Per @sandersn this pattern is recognized in the binder and only uses syntactic information, therefore does not use the type information from testPrototype .
Related Issues: #39167 from same user code. Also #33454 maybe?
Code
var test = {};
test.testPrototype = {
add: function (i) {
}
}
test.class = function (name) {
function getName() {
return name;
}
this.name = getName();
}
test.class.prototype = test.testPrototype;
var t = new test.class("test");
t.name
t.add // EXPECTED: Binds to `add` from the prototype, ACTUAL: doesn't
//
// Same pattern works when a literal is assigned to the prototype:
//
var test2 = {};
test2.class = function (name) {
function getName() {
return name;
}
this.name = getName();
}
// replaced `test.testPrototype` with a literal
test2.class.prototype = {
add: function (i) {
}
};
var t2 = new test2.class("test");
t2.name
t2.add // ACTUAL: `add` bound correctly
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"checkJs": true,
"allowJs": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"moduleResolution": 2,
"target": "ES2017",
"jsx": "React",
"module": "ESNext"
}
}
Playground Link: Provided
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided TypeScript Playground reproduction and the binder behavior described in the issue, comparing the object-literal prototype assignment with the assignment from test.testPrototype. The work is done when the type of the initializer supplies prototype properties so t.add is recognized in the example, while the existing literal case continues to work.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, typescript
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100