我想在基于打字稿的项目中在自己的文件中定义几个接口,我将从中实现用于生产的类以及用于测试的模拟。但是,我无法弄清楚正确的语法是什么。我已经找到了很多关于声明接口和实现它们的教程,但是它们在同一个文件中都有一个简单的接口和派生类的实现,这不是非常真实的。导出和导入接口的正确方法是什么?
答案 0 :(得分:53)
You need to export the interface from the file in which is defined and import it wherever you want to use it.
in IfcSampleInterface.ts
:
export interface IfcSampleInterface {
key: string;
value: string;
}
In SampleInterface.ts
import { IfcSampleInterface } from './IfcSampleInterface';
let sampleVar: IfcSampleInterface;
答案 1 :(得分:25)
答案 2 :(得分:10)
在不传播多个导出的情况下,您可以将它们分为一个
public class MainActivity extends AppCompatActivity {
public static final String SHARED_PREF="SHARED_PREFS";
public static final String KEY="KEY_TEXT";
private EditText editText;
private Button button;
private Button button2;
private TextView final_text;
private TextView editText3;
private int val_two;
private String food_list;
private String id;
private NotificationChannel channel;
@Override
protected void onCreate(Bundle savedInstanceState) {
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
channel = new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "notifChan2", NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("A calorie notification");
}
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String val = editText.getText().toString();
String food = editText3.getText().toString();
food_list.concat(food);
val_two = val_two + Integer.parseInt(val);
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF,0);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(KEY, val_two);
editor.apply();
String finalText = "You current calories " + val_two ;
if(val_two>1000){
DisplayNotification();
}
final_text.setText(finalText);
callupdatewidget() function this will print the current calories into the wdiget
callUpdateWidget(MainActivity.this,finalText);
}
});
}
private void DisplayNotification() {
NotificationCompat.Builder notify = new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_menu_agenda)
.setContentTitle("Congrats!")
.setContentText("You have reached your calorie goal of 1000!");
NotificationManagerCompat notificationMgr = NotificationManagerCompat.from((this));
notificationMgr.notify(1, notify.build());
}
}
块(在这种情况下,没有文件export {}
类型应为声明):
default
// interfaces.ts
interface IWords {
[key: string]: string;
}
interface INumbers {
[key: string]: number;
}
interface IBooleans {
[key: string]: boolean;
}
interface IValues {
[key: string]: string | number;
}
interface IStructures {
[key: string]: INumbers | IBooleans | IValues;
}
export {
// not exporting IWords | INumbers
IBooleans,
IValues,
IStructures,
}
答案 3 :(得分:2)
您需要导出已定义的文件中的接口,并将其导入到所使用的文件中。有关示例,请参阅此链接。
x.ts
interface X{
...
}
export default X
y.ts
import X from "./x.ts"
// You can use X now
有关详细信息,请参阅https://www.typescriptlang.org/docs/handbook/modules.html
答案 4 :(得分:1)
您可以在相对较新的项目中使用以下语法
`import type { xxx } from './xxx'`
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html