如何在打字稿中定义通用接口

时间:2020-03-06 02:38:48

标签: typescript

我想在一个函数中定义具有不同形式接口的有效载荷。

每个有效负载如下,我已经定义了“ UpdatePayload”接口,如下所示:

vector<Record> records;
vector<Record*> sortedRecords;

// populate records as needed...

dmaArr(sortedRecords, records);

// use sortedRecords as needed...

但是,函数内部的代码会产生以下错误:

/*
ex) payload interface
const interface updateBodyPayload = {
  title: ...
  content: ...
}
*/

/*
ex) payload interface
const interface updateStatePayload = {
  type: ..
  count: ..
}
*/

// Payload Merge interface
export type updatePayload = Partial<updateBodyPayload | updateStatePayload>;

我犯了什么错误?

1 个答案:

答案 0 :(得分:2)

如果您想让import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(MyApp()); SystemChrome.setSystemUIOverlayStyle( SystemUiOverlayStyle( statusBarColor: Colors.transparent, statusBarBrightness: Brightness.light), ); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: ('SplashScreeen'), home: MySplashScreen(title: 'SplashScreen'), ); } } class MySplashScreen extends StatefulWidget { MySplashScreen({Key key, this.title}) : super(key: key); final String title; @override _MySplashScreenState createState() => _MySplashScreenState(); } class _MySplashScreenState extends State<MySplashScreen> with TickerProviderStateMixin { AnimationController fadeAnimationLogoController; AnimationController moveUpAnimationLogoController; Animation<double> fadeAnimationLogo; Animation<Offset> moveUpAnimationLogo; bool showWelcome = false; initState() { super.initState(); fadeAnimationLogoController = AnimationController(duration: Duration(seconds: 5), vsync: this); moveUpAnimationLogoController = AnimationController( duration: Duration(seconds: 5), vsync: this, ); fadeAnimationLogo = CurvedAnimation( parent: fadeAnimationLogoController, curve: Curves.easeIn); moveUpAnimationLogo = Tween<Offset>( begin: Offset(0, 0), end: Offset(0, -0.2), ).animate(moveUpAnimationLogoController); fadeAnimationLogoController.forward(); fadeAnimationLogoController.addListener(() { if (fadeAnimationLogo.status == AnimationStatus.completed) { moveUpAnimationLogoController.forward(); } }); moveUpAnimationLogoController.addListener(() { if (moveUpAnimationLogo.status == AnimationStatus.completed) { //moveUpAnimationLogoController.forward(); setState(() { showWelcome = true; }); } }); } @override Widget build(BuildContext context) { return Scaffold( body: Column( children: <Widget>[ Container( color: Colors.white, child: FadeTransition( opacity: fadeAnimationLogo, child: SlideTransition( position: moveUpAnimationLogo, child: Column( mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Image( image: AssetImage('assets/images/csrhuntlogo.png'), height: 300, width: 300, ), Text( ('C S R H U N T'), style: TextStyle( fontFamily: 'League Spartan', height: 1, fontSize: 34, color: Colors.black, decoration: TextDecoration.none, ), ), Text( ('FIND PLAY EARN'), style: TextStyle( fontFamily: 'Montserrat', height: 1, fontSize: 15, color: Colors.black, decoration: TextDecoration.none, ), ), ], ), ), ), ), showWelcome ? Expanded( child: WelcomeScreen( title: "test", ), ) : Container(), ], ), ); } } class WelcomeScreen extends StatefulWidget { WelcomeScreen({Key key, this.title}) : super(key: key); final String title; @override _WelcomeScreenState createState() => _WelcomeScreenState(); } class _WelcomeScreenState extends State<WelcomeScreen> with SingleTickerProviderStateMixin { AnimationController fadeAnimationWelcomeController; Animation<double> fadeAnimationWelcome; @override void initState() { fadeAnimationWelcomeController = AnimationController( vsync: this, duration: Duration(milliseconds: 2000), ); fadeAnimationWelcome = CurvedAnimation( parent: fadeAnimationWelcomeController, curve: Curves.easeIn); super.initState(); fadeAnimationWelcomeController.forward(); } @override Widget build(BuildContext context) { return Container( color: Colors.white, child: FadeTransition( opacity: fadeAnimationWelcome, child: Stack( children: <Widget>[ Positioned( top: 590, left: 20, child: SizedBox( width: 350.0, height: 50.0, child: RaisedButton( color: new Color.fromRGBO(255, 213, 0, 1.0), textColor: Colors.black, onPressed: () {}, child: Text( 'log in', style: TextStyle( height: 1, fontSize: 25, fontFamily: 'League Spartan', ), ), shape: RoundedRectangleBorder( borderRadius: new BorderRadius.circular(18.0), side: BorderSide(color: Colors.transparent), ), ), ), ), Positioned( bottom: 50, left: 20, child: SizedBox( width: 350.0, height: 50.0, child: RaisedButton( color: new Color.fromRGBO(255, 213, 0, 1.0), textColor: Colors.black, onPressed: () {}, child: Text( 'Sign up', style: TextStyle( height: 1, fontSize: 25, fontFamily: 'League Spartan', ), ), shape: RoundedRectangleBorder( borderRadius: new BorderRadius.circular(18.0), side: BorderSide(color: Colors.transparent), ), ), ), ), ], ), ), ); } } 输入合并形式updatePayloadupdateBodyPayload的结果,那么它必须类似于:(updateStatePayload而不是{{1} })

&

现在|将会是:

type updatePayload = Partial<updateBodyPayload & updateStatePayload>;