Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎么在接口多态中定义一个含可选属性的对象? #327

Closed
hydrz opened this issue Nov 7, 2019 · 1 comment
Closed

怎么在接口多态中定义一个含可选属性的对象? #327

hydrz opened this issue Nov 7, 2019 · 1 comment
Labels
bug

Comments

@hydrz
Copy link

@hydrz hydrz commented Nov 7, 2019

bug描述
我想在接口多态中定义一个对象是含可选属性的, 但是使用interface来定义对象会要求需要实现,使用type定义对象似乎不能实现可选属性的方式.示例代码如下:

复现bug的步骤

  1. 使用interface定义对象
<script cml-type="interface">
type Callback = (res ? : any) => void;

interface Options {
  success ? : Callback;
  fail ? : Callback;
  complete ? : Callback;
}

interface BluetoothInterface {
  /**
   * 获取本机蓝牙适配器状态
   */
  getBluetoothAdapterState(object ? : Options): void;
}
</script>
  1. 使用type定义对象,并用可选的问题方式
<script cml-type="interface">
type Callback = (res ? : any) => void;

type Options = {
  success ? : Callback;
  fail ? : Callback;
  complete ? : Callback;
}

interface BluetoothInterface {
  /**
   * 获取本机蓝牙适配器状态
   */
  getBluetoothAdapterState(object ? : Options): void;
}
</script>
  1. 使用type定义对象,并用联合属性类型方式
<script cml-type="interface">
type Callback = (res ? : any) => void | undefined;

type Options = {
  success: Callback;
  fail: Callback;
  complete: Callback;
}

interface BluetoothInterface {
  /**
   * 获取本机蓝牙适配器状态
   */
  getBluetoothAdapterState(object ? : Options): void;
}
</script>

问题截图
image

编译环境信息

  • chameleon-tool 版本:[1.0.3]
  • nodejs版本、npm版本:[node v10.15.3、npm 6.4.1]
  • 电脑操作系统: [Deepin 15.11]

运行环境信息

  • 端版本: [微信开发者工具 https://github.com/cytle/wechat_web_devtools 1.02.1910121]
@hydrz hydrz added the bug label Nov 7, 2019
@jimwmg

This comment has been minimized.

Copy link
Collaborator

@jimwmg jimwmg commented Nov 8, 2019

1 首先在 chameleon.config.js 中开启配置

check: {
    enable: true,
    enableTypes: ["Object","Array","Nullable"]
  },

具体参考 https://cmljs.org/doc/framework/config.html

2 在你的 interface 文件中

<script cml-type="interface">
type Callback = (res ? : any) => void;
type Options = {
  success: Callback,
  fail:? Callback,
  compelete:Callback,
}
interface InterInterface {
  getBluetoothAdapterState(options: Options): Void;
}

</script>

<script cml-type="web">

class Method implements InterInterface {
  getBluetoothAdapterState(options) {
    if(options.success){
      options.success();
    }
    if(options.fail){
      options.fail();
    }
    if(options.compelete){
      options.compelete();
    }
  }
}

export default new Method();
</script>

3 在组件文件中,引入interface 之后,调用

import common from '../xxx/to/interface';
common.getBluetoothAdapterState({
        success:function(){
          console.log("success");
        },
        // fail:function(){
        //   console.log('fail')
        // },
        compelete:function(){
          console.log('complete');
        }
      });
@jimwmg jimwmg closed this Nov 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.