Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

reactjs - Import types two different project

I have a project which includes two sub projects. Web for web(react), native for RN. I have common styles for components, then I created interfaces. Developer could write web or native style codes. The following interface has buttonStyle. buttonStyle React.CSSProperties type for Web, ViewStyle type for RN. My issue is I cannot import these interface from common interface. I have to import these interfaces two project. It causes code duplicate. How can I import these interfaces from common file.

Project structure

common interface for button

question from:https://stackoverflow.com/questions/65949998/import-types-two-different-project

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In order to have a common style that applied to both the web and the native versions you need to make sure that it passes the type checks for both types. What you have now requires that buttonStyle is assignable to React.CSSProperties or ViewStyle. You need to require that it is assignable to both in order to use that style on a View and a button. Just switch your | (which is a union) to & (which is an intersection).

export interface IButtonStyle extends INavigationStyle {
    buttonStyle: React.CSSProperties & ViewStyle;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...