microsoft / microsoft/TypeScript

Prototype assignment should take the type of the initializer and not require a literal

Offen
#39,166 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit der bereitgestellten TypeScript Playground-Reproduktion und dem im Issue beschriebenen binder-Verhalten und vergleiche dabei die Prototypzuweisung des Objektliterals mit der Zuweisung aus test.testPrototype. Die Arbeit ist abgeschlossen, wenn der Typ des Initialisierers Prototyp-Eigenschaften bereitstellt, sodass t.add im Beispiel erkannt wird, während der vorhandene Literal-Fall weiterhin funktioniert.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.