Coder Social home page Coder Social logo

Comments (5)

janhesters avatar janhesters commented on April 25, 2024 1

Add undefined to the type: keyboardDidShowListener?: EmitterSubscription or keyboardDidShowListener: EmitterSubscription | undefined. Properties whose type include undefined skip the strictPropertyInitialization verification completely.

I tried this, but since I use .remove() I'll get a linting error there becuase undefined does not have that function.

If what you're doing in componentDidMount can be safely moved to the constructor without breaking anything, moving it would fix the error and still ensure type safety.
It seems to be feasible, since Platform and Keyboard are imported from react-native and do not seem to be attached to the components' instance in anyway (I haven't personally ever done anything with React Native so take this with a grain of salt). There may also be performance issues if React ever instantiates your component without mounting it, therefore never calling componentWillUnmount.

This seems about right, which is why I used componentDidMount().

You can overrule that verification by adding a ! after the field name, as such: keyboardDidShowListener!: EmitterSubscription. That basically tells Typescript that even though there is no explicit initialization, you are sure that it will be initialized (as it is).
An example of a situation where this wouldn't work is if you were defining a field that was going to be used inside render, but would be undefined in the first render (before calling componentDidMount).

This also has the problem that .remove() throws a linting error.
Therefore I chose to set strictPropertyInitialization: true. Thank you very much for your help, you helped me out tremendously!

from react.

vhfmag avatar vhfmag commented on April 25, 2024

Your fix was correct, the issue is that you have the strict flag (or the more specific strictPropertyInitialization flag) activated on tsconfig.json and didn't initialize that field either inline or in a constructor. There are three ways for you to fix it:

  • If what you're doing in componentDidMount can be safely moved to the constructor without breaking anything, moving it would fix the error and still ensure type safety.
    • It seems to be feasible, since Platform and Keyboard are imported from react-native and do not seem to be attached to the components' instance in anyway (I haven't personally ever done anything with React Native so take this with a grain of salt). There may also be performance issues if React ever instantiates your component without mounting it, therefore never calling componentWillUnmount.
  • You can overrule that verification by adding a ! after the field name, as such: keyboardDidShowListener!: EmitterSubscription. That basically tells Typescript that even though there is no explicit initialization, you are sure that it will be initialized (as it is).
    • An example of a situation where this wouldn't work is if you were defining a field that was going to be used inside render, but would be undefined in the first render (before calling componentDidMount). Which brings us to the last option:
  • Add undefined to the type: keyboardDidShowListener?: EmitterSubscription or keyboardDidShowListener: EmitterSubscription | undefined. Properties whose type include undefined skip the strictPropertyInitialization verification completely.

I hope that can be helpful :). To read more about that strictPropertyInitialization, check https://blog.mariusschulz.com/2018/05/20/typescript-2-7-strict-property-initialization.

from react.

vhfmag avatar vhfmag commented on April 25, 2024

Happy to help! :)

from react.

swyxio avatar swyxio commented on April 25, 2024

glad you got helped!!!! keeping this open hopefully to incorporate some insights from this in a future addition

from react.

stale avatar stale commented on April 25, 2024

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions!

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.