admin管理员组文章数量:1242842
I'm fairly new to either react and typescript. and in the world of PHP it is remended to declare all of your classes, interfaces, abstracts and... in their own separate file which is remended in SOLID principles too.
so I was wondering that should I declare propsInterface and stateInterface for each of my statefull ponents or it is fine to declare them in the same file. so my questions is:
1: which one is the preferred way in js/typescript
2: does declaring at least two interface (props and states) for each statefull ponent going to slow down the performance or it just don't matter because piled js file doesn't contain those interfaces ?
I'm really confused
I'm fairly new to either react and typescript. and in the world of PHP it is remended to declare all of your classes, interfaces, abstracts and... in their own separate file which is remended in SOLID principles too.
so I was wondering that should I declare propsInterface and stateInterface for each of my statefull ponents or it is fine to declare them in the same file. so my questions is:
1: which one is the preferred way in js/typescript
2: does declaring at least two interface (props and states) for each statefull ponent going to slow down the performance or it just don't matter because piled js file doesn't contain those interfaces ?
I'm really confused
- which one is the preferred way in js/typescript - there's no preferable way in JS because types don't exist in JS. does declaring at least two interface (props and states) for each statefull ponent going to slow down the performance - I'd suggest to read how TS works before moving any further. Types don't exist at runtime. They cannot affect performance. – Estus Flask Commented Dec 2, 2018 at 21:20
1 Answer
Reset to default 16I do it in the same file, unless I need to reuse it. I prefer this approach because the interfaces that don't need to be used outside can remain 'private'.
For example in React (your question says you use it), the interface for internal state is rarely reused, so you can have it just in the ponent without the need of exporting/exposing it (and being misused by another ponent). If the interface is reused (sometimes happens with the props one), I take it to a separate file.
It doesn't affect performance at all, because in the final piled js, all those abstractions disappear (interfaces, types, etc).
There isn't a hard rule for this, just sharing my preference.
Cheers, from La Paz, Bolivia
PS: I've been heading a big project for a while (with React and TS)
本文标签:
版权声明:本文标题:javascript - React-Typescript, declare interface in separate files or in the same file, which one better? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740153436a2233089.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论