microsoft / microsoft/TypeScript

Enforce definite initialization of `static` members

Open
#27,899 10 comments 24 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file or test is named in the issue. Reproduce the TypeScript 3.2 behavior using the provided Playground link and the class example, then inspect the compiler's handling of strictPropertyInitialization and static members. Done means both reads of the uninitialized static member produce compile errors without breaking valid inheritance or initialization semantics.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.