microsoft / microsoft/TypeScript

Set context of this keyword in definition file

Aperta
#29,758 8 commenti 6 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

Search Terms

set this context in typescript definition file

Suggestion

A way to change the context of this keyword in a type definition file. This is admittedly a weird feature since, to my knowledge, it doesn't really align with how modules work. Perhaps there is another way to achieve this use case.

Use Cases

Here's a video of how my application works - https://vimeo.com/315553984

I have an application that allows users to write code which will then be executed via the Function API - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
Here's a simplified version of how the app works at its core -

const userCode = `
  console.log(this, foo, bar);
`;
const func = Function('foo', 'bar', userCode);
func.call({myThis: 'hello'}, 'fooString', 'barString');

My app allows users to write their code in Monaco editor and I've successfully added type definitions for the foo and bar variables via the addExtraLib method - https://microsoft.github.io/monaco-editor/api/interfaces/monaco.languages.typescript.languageservicedefaults.html

So when the user starts to type foo intellisense informs them that it is a string. I'd like to do the same for the this keyword.

Examples

See use cases ^

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 esaminando come i file di definizione TypeScript rappresentano la parola chiave this e come queste informazioni arrivano al servizio linguistico di Monaco tramite addExtraLib. Usa l’esempio dell’API Function come caso d’uso di riferimento e verifica che IntelliSense dell’editor possa dedurre il contesto this richiesto senza modificare il comportamento di runtime di JavaScript.

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

Valutazione

Stack tecnologico
javascript, typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.