You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
export class TestCorrect {
static readonly someName = "someName";
}
export class TestBroken {
static readonly someName: "someName";
// spot the error? ----^
// note how similar the syntax looks to the class above.
// but this time we are declaring a literal type and also prevent any assignment. which makes little sense and is likely a bug.
// what was meant is probably the code above.
}
if (TestBroken.someName == "someName") {
console.log(`uo, i'm unreachable. Also the type of TestBroken.someName is `"someName"`, where it should rather be `undefined` or an error at the class definition)
}
π Actual behavior
the code is accepted by typescript
π Expected behavior
either an error at TestBroken-class or a type inference of "someName" | undefined (first option preferred)
The text was updated successfully, but these errors were encountered:
@MartinJohns thanks! I think for the mutable version it would be hard for typescript to determine if the property gets initialized somewhere, but it could be typed as "literal" | undefined in that case. And if strictPropertyInitialization is enabled or readonly is specified it should require a static assignment in my opinion.
RyanCavanaugh
changed the title
static readonly property is not typechecked
Issue error if readonly static property isn't initialized
Apr 30, 2021
RyanCavanaugh
changed the title
Issue error if readonly static property isn't initialized
Issue error if readonly static property has no initializer
Apr 30, 2021
Bug Report
π Search Terms
static property readonly undefined typecheck literal
π Version & Regression Information
As far as i know this is the behavior of all typescript version, but i think it's not intuitive
β― Playground Link
See my Playground example:
https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwDYEMDOa4BVhpgYWimAXgG8BYAKDlrjxRgEsE5iUATCAOyQE96EALbAAcihFwAvHABEaYWInBZAbmoBfatVCRYiVBmy4YAISgQA1sG5xKNOg2at2XXgIUjxIgFxzPSiJqmtpUOHiEUMSkAHQB3sDqVNRMAGZwABThZhbW3HGKCdIy8oXKsgCU9nSIPApIwDFIEADmGQAGAK4QADRwTADkQnCd3OwIABYoAEYNMXAAgkgKcDATwKt8YBsQ6dnmVjYFXsr9mAOlJ0EDfQDu68T98GgTEJ1IHHDTG4xwDejwAZ4KBMbgtOAAHxG3A4wFSoOAHAGcGgbEYDzgA1GsPh3ERA3aFU0QA
π» Code
π Actual behavior
the code is accepted by typescript
π Expected behavior
either an error at
TestBroken
-class or a type inference of"someName" | undefined
(first option preferred)The text was updated successfully, but these errors were encountered: