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

Suggestion/feature request - enforce static properties/fields #21976

Closed
ORESoftware opened this issue Feb 15, 2018 · 2 comments
Closed

Suggestion/feature request - enforce static properties/fields #21976

ORESoftware opened this issue Feb 15, 2018 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@ORESoftware
Copy link

I added this question on SO:

is there a way to enforce static class properties? https://stackoverflow.com/questions/48813814/enforce-static-member-in-class-using-enum

It doesn't look like there is a way to do what I want to do, hence the feature request.

Code

declare enum Foods {
  CHERRY = 'CHERRY',
  LETTUCE = 'LETTUCE',
  JERKY = 'JERKY'
}

declare abstract class ForceFoods {
  static food : Foods
}

export class MyFoods implements ForceFoods {
  static food = Foods.CHERRY;  // if I omit this line, tsc will not complain
}

Expected behavior:
I was hoping tsc could enforce static properties to be defined

Actual behavior:
I don't currently believe tsc can enforce static properties

@mhegazy
Copy link
Contributor

mhegazy commented Feb 15, 2018

Now that we have --strictPropertyInitialization and ! operator for asserting definite assignment, we should consider making an uninitialized static property an error.

That does not solve your problem thought.. it will give you an error on ForceFoods as well as on MyFoods. I think you are looking for #14600 for abstract static properties/methods.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Feb 15, 2018
@RyanCavanaugh
Copy link
Member

This is a little jumbled up with abstract but it seems like this is a duplicate of #27899

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Jun 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants