microsoft / microsoft/TypeScript

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

Aperta
#39,166 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia con la riproduzione fornita in TypeScript Playground e con il comportamento del binder descritto nell’issue, confrontando l’assegnazione del prototipo dell’object literal con l’assegnazione da test.testPrototype. Il lavoro è completato quando il tipo dell’inizializzatore fornisce le proprietà del prototipo, in modo che t.add venga riconosciuto nell’esempio, mentre il caso di literal esistente continua a funzionare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.