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
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.
Most helpful comment
Now that we have
--strictPropertyInitializationand!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
ForceFoodsas well as onMyFoods. I think you are looking for https://github.com/Microsoft/TypeScript/issues/14600 for abstract static properties/methods.