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

Enforce definite initialization of static members #27899

Open
bcherny opened this issue Oct 15, 2018 · 8 comments
Open

Enforce definite initialization of static members #27899

bcherny opened this issue Oct 15, 2018 · 8 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@bcherny
Copy link

bcherny commented Oct 15, 2018

Is there some clever reason why TSC can't check this (maybe... related to semantics of JS inheritance?)?

TypeScript Version: 3.2.0-dev.20181011

Search Terms: strictPropertyInitialization, static, initialized, uninitialized, assigned, property

Code

class A {
  static s: number
  a() {
    A.s * 3 // Should be a compile error
  }
}
A.s * 7 // Should be an compile error

Expected behavior: Both lines should produce compile errors.

Actual behavior: Both lines result in runtime exceptions.

Playground Link

Related Issues:

@ghost
Copy link

ghost commented Oct 16, 2018

Is the suggestion that we should always error on a static field with no initializer?

@weswigham weswigham added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Oct 16, 2018
@bcherny
Copy link
Author

bcherny commented Oct 17, 2018

@andy-ms Would it be possible to error when I use a static field that hasn't been definitely assigned? Or, if this isn't a common enough pattern to support, then your suggestion is spot on. Either one feels safer than the current behavior to me.

Eg.

class A {
  static s: number
}

if (x) {
  A.s = 3
  // OK to read A.s in this branch
}
// NOT OK to read A.s in this branch

@jcalz
Copy link
Contributor

jcalz commented Feb 24, 2019

Related stackoverflow question

I can't think of a decent workaround either. Any ideas?

@SlurpTheo
Copy link

SlurpTheo commented Apr 10, 2019

Why were static properties specifically excluded from --strictPropertyInitialization?

@Yogu
Copy link
Contributor

Yogu commented Aug 29, 2019

Is the suggestion that we should always error on a static field with no initializer?

Same rules as for non-static members should apply - if the type does not allow undefined, the property needs to be initialized, otherwise it would have a value that's not allowed.

@weswigham What kind of feedback do you await?

I just discovered a very dumb bug that would have been caught by this check:

export class JSONPreprocessorType implements MessagePreprocessorType {
    static contentType: 'application/json';
}

(It should have been an = instead of :.)

@Skalman
Copy link

Skalman commented Jun 6, 2020

I would prefer to have the error early on - at the site of the declaration. Usually you can't figure out the usage anyway (since it's in another file).

I recently had the same problem as @Yogu, and was thoroughly surprised by strictNullChecks not catching this problem.

@JeanMeche
Copy link
Contributor

I just ran into this bug following a type (: instead of =). A fix would be welcome =)

@lazytype
Copy link

lazytype commented Jan 3, 2023

Since class static blocks were introduced in 4.4, it seems like TS can use the same heuristic that's used for instance variables and constructors:

class Foo {
  // Suggested error: Property 'instanceValue' has no initializer and is not definitely assigned in a static block.
  static staticValue: number; 

  // @ts-expect-error "Property 'instanceValue' has no initializer and is not definitely assigned in the constructor.(2564)"
  instanceValue: number;

  static {
    // this.staticValue = 42;
  }

  constructor() {
    // this.instanceValue = 42;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

8 participants