microsoft / microsoft/TypeScript

[suggest] constructor init delegation with "strictPropertyInitialization"

Ouverte
#59,997 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

🔍 Search Terms

strictPropertyInitialization, Property has no initializer and is not definitely assigned in the constructor.

✅ Viability Checklist
⭐ Suggestion

Hello, have you ever considered adding a special hack to delegate the role of a class constructor when using strictPropertyInitialization: true in the tsconfig file?

In fact, I have an issue with an architecture that makes the constructor unnecessary until the class is injected into a facade that activates the system and creates the private dependencies of the class.

There is no other solution except to disable this rule, which is frankly not great and very limiting when a project uses several design patterns requiring a distinct approach.
To take advantage of the security of strictPropertyInitialization, would it be possible to have maybe a special utility type that tells TypeScript to check this method instead of the constructor?

Here are examples that could potentially be good solutions:
I personally prefer examples 2 and 3.
The first example can bring more complexity


📃 Motivating Example

Suggest 1: method with special type

We add methodname( this:ThisConstructor )

class SystemA {
	public readonly instanceNeedDependancyAvaibleLater: object; // Property 'instanceNeedDependancyAvaibleLater' has no initializer and is not definitely assigned in the constructor.

	constructor() {
	}

	// ThisConstructor can be a special type flag thas tell to tsserveur with "strictPropertyInitialization" to ignore the constructor and scan asignation in this method instead
	init( this:ThisConstructor ) {
		this.instanceNeedDependancyAvaibleLater = {};
	}
}

issues:

  • can add complexity
  • can add perf issue with ts (need scan all method)
  • issue with 2 or more !
  • may have issue with overload

Suggest 2: detect with illegal this in constructor.
It is currently illegal to use "this" in the constructor.
We could take this opportunity to tell ts to check another methods for props initialisation.
I using "string" here, but is juste for give the idea.

class SystemA {
	public readonly instanceNeedDependancyAvaibleLater: object; // Property 'instanceNeedDependancyAvaibleLater' has no initializer and is not definitely assigned in the constructor.

	//  this:'methodName' can maybe tell ts to find this method and scan assignation instead use this constructor
	constructor( this:'init' ) {
	}

	init( ) {
		this.instanceNeedDependancyAvaibleLater = {};
	}
}

issues:

  • will maybe not work with protected and private method based ont how ts work
  • rename or security issue if using "string", ts will need index the token or suggest types
  • add complexity in constructor arguments
  • issue if we extends class with constructor params "need implement"

Suggest 3: using comment

class SystemA {
	public readonly instanceNeedDependancyAvaibleLater: object; // Property 'instanceNeedDependancyAvaibleLater' has no initializer and is not definitely assigned in the constructor.

	//  using a comment flag to scan .init method instead of constructor
	//@ts-strictPropertyInitialization {SystemA.prototype.init}
	constructor( ) {
	}

	init( ) {
		this.instanceNeedDependancyAvaibleLater = {};
	}
}

issues:

  • will maybe not work with protected and private method based ont how ts work
  • It may be perceived and treated incorrectly as a comment, which adds ambiguity:
    • Is it a comment or an instruction essential to the execution of the code? This comment therefore guides the interpretation of the code?

Suggest 4: tracking the flow from constructor:
https://github.com/microsoft/TypeScript/issues/32194
https://github.com/microsoft/TypeScript/issues/30462

class SystemA {
	public readonly instanceNeedDependancyAvaibleLater: object; // Property 'instanceNeedDependancyAvaibleLater' has no initializer and is not definitely assigned in the constructor.

	constructor() {
		this.init();
	}

	// ThisConstructor can be a special type flag thas tell to tsserveur with "strictPropertyInitialization" to ignore the constructor and scan asignation in this method instead
	init() {
		this.instanceNeedDependancyAvaibleLater = {};
	}

issues:

  • can add perf issue with ts (need scan all method and what they do)
  • can add complexity with multiple methods: who, why do assignments
  • it maybe a breakchange
💻 Use Cases
  1. What do you want to use this for?
    This is to address the need for an architecture that requires initialization after adding a loosely coupled facade.
    The idea is to maintain the security of declarations, which current solutions do not allow.
    This approach can also open the door to design patterns that are difficult to manage with TypeScript.

  2. What shortcomings exist with current approaches?
    Currently, there are various tricks that do not allow to maintain the security and philosophy of TypeScript.
    1: //@ts-ignore
    2: public props!:object
    3: strictPropertyInitialization:false

  3. What workarounds are you using in the meantime?
    public props!:object

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Aucun fichier source, test ou point d’entrée du compilateur n’est indiqué. Commencez par examiner strictPropertyInitialization et les issues associées #32194 et #30462 ; le travail serait considéré comme terminé lorsqu’une conception arrêtée de l’initialisation déléguée et du comportement TypeScript correspondant serait définie.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
20/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.