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

Static class fields can lead to runtime errors #42901

Closed
millsp opened this issue Feb 20, 2021 · 2 comments
Closed

Static class fields can lead to runtime errors #42901

millsp opened this issue Feb 20, 2021 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@millsp
Copy link
Contributor

millsp commented Feb 20, 2021

Bug Report

It is permitted not to initialize a static field:

class Foo {
    static bar: string
}

const baz = Foo.bar // inferred string

console.log(typeof baz) // is undefined

🔎 Search Terms

static class field property undefined error assigned

🕗 Version & Regression Information

This bug has always been here, as far as I checked (3.3.3).

  • This is a crash
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Class.

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo {
    static bar: string
}

const baz = Foo.bar // string

console.log(typeof baz) // undefined

🙁 Actual behavior

undefined values will certainly lead to a program crash.

🙂 Expected behavior

Either to be suggested to assign a default value or to mark the field as optional.

@millsp millsp changed the title static class fields can unexpectedly be undefined at runtime Static class fields can lead to runtime errors Feb 20, 2021
@MartinJohns
Copy link
Contributor

Duplicate of #27899. Search terms: static undefined

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 22, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants