Typescript: Suggestion/feature request - enforce static properties/fields

Created on 15 Feb 2018  路  1Comment  路  Source: microsoft/TypeScript

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

In Discussion Suggestion

Most helpful comment

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 https://github.com/Microsoft/TypeScript/issues/14600 for abstract static properties/methods.

>All comments

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 https://github.com/Microsoft/TypeScript/issues/14600 for abstract static properties/methods.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

wmaurer picture wmaurer  路  3Comments

MartynasZilinskas picture MartynasZilinskas  路  3Comments

DanielRosenwasser picture DanielRosenwasser  路  3Comments

blendsdk picture blendsdk  路  3Comments

seanzer picture seanzer  路  3Comments