Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically Assign Constructor Parameters as Class Properties in TypeScript #61457

Closed
6 tasks done
Phaneendra-Pallapothu opened this issue Mar 20, 2025 · 1 comment
Closed
6 tasks done
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@Phaneendra-Pallapothu
Copy link

Phaneendra-Pallapothu commented Mar 20, 2025

🔍 Search Terms

Currently, in TypeScript, when we define a constructor (concise way), parameters do not automatically become class properties unless explicitly marked with public, private, or protected.

✅ Viability Checklist

⭐ Suggestion

I propose that constructor parameters should default to public if no access modifier is specified.

This means:
-> Explicit access modifiers (private, protected) should still behave as expected.
->If no modifier is given, TypeScript should automatically treat the parameter as public.

📃 Motivating Example

Current Behaviour:
class Student {
constructor(public name: string, public age?: number, public address?: string) {}
}

Suggested Behaviour:
class Student {
constructor(name: string, age?: number, address?: string) {}
}

💻 Use Cases

Why This Change?
✅ Reduces Redundancy – No need to explicitly write public when it's the default behavior.
✅ Improves Developer Experience – Constructor parameters are typically meant to be class properties, so it makes sense for TypeScript to assume public.
✅ Keeps Code Clean & Readable – Avoids unnecessary boilerplate.

@MartinJohns
Copy link
Contributor

This would be a massive huge breaking change, without an apparent opt-out.

It would also violate this point from the Viability Checklist:

This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)

(The existing behavior violates this already, but predates the decision to not add any more runtime functionality.)

Constructor parameters are typically meant to be class properties,

I disagree. There are many cases where this is not the case.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants