【问题标题】:How to typescript file creating using with interfaces method other typescript file?如何使用接口方法创建其他打字稿文件的打字稿文件?
【发布时间】:2018-01-06 23:11:10
【问题描述】:

我正在使用 typescript 处理本机项目。特别是使用链接this

创建一个权限文件

我基本上有一个相机和存储方法,对于如何在我的项目中创建接口模块有点困惑

const Permission = require('react-native-permissions')
import React from 'react'
import {
StyleSheet,
TouchableHighlight,
Text,
View,
Alert,
AppState,
Platform,
} from 'react-native'

interface Props extends {
string: []
}

interface PermissionsState extends {
status: {},
}

class Permissions extends React.Component<Props,PermissionsState> {
constructor(props) {
Permission.check([type])
type PermissionStatus = 'granted' | 'denied' |'never_ask_again';



this.state = {

};
}

render () {
return (
render() {
return (
<View>
<View/>
)
  }
}





 export default new Permissions()

我的错误开始于interface Props extends { 行,错误描述是:

接口只能使用可选类型参数扩展标识符/限定名。 (属性)字符串:未定义[]

【问题讨论】:

    标签: reactjs typescript react-native permissions


    【解决方案1】:

    您所要做的就是从两个接口的声明中删除 extends 关键字。因为它们实际上并没有扩展任何东西,并且反应本身不需要它们实现/扩展任何其他接口/类。

    所以你的代码看起来像这样:

    const Permission = require('react-native-permissions');
    import React from 'react';
    import {
            StyleSheet,
            TouchableHighlight,
            Text,
            View,
            Alert,
            AppState,
            Platform,
        } from 'react-native';
    
    interface Props
    {
        names: string[];
    }
    
    type PermissionStatus = 'granted' | 'denied' |'never_ask_again';
    
    class PermissionsState
    {
        public status: PermissionStatus;
    }
    
    export class Permissions extends React.Component<Props, PermissionsState>
    {
        constructor(props: Props, context: any)
        {
            super(props, context);
    
            this.state = new PermissionsState();
        }
    
        public render()
        {
            return(<View/>);
        }
    }
    

    【讨论】:

    • 这一行开头的一些错误;导出类权限....错误的名称是:类“权限”错误地扩展了基类“组件”。属性“渲染”的类型不兼容。类型 '() => boolean' 不可分配给类型 '() => false |元素'。类型 'boolean' 不能分配给类型 'false |元素'。
    • 确保在文件中声明您的代码,扩展名为tsx。不是ts
    • 我还有其他ts格式的文件,哪个文件是我用的接口模块,运行流畅。
    • 但是改变了文件格式 tsx 没问题但是错误仍然是模块。
    • 如果你想使用像&lt;View/&gt;这样的jsx/tsx语法糖,你必须使用tsx
    猜你喜欢
    • 2020-08-14
    • 1970-01-01
    • 2017-12-28
    • 2017-11-28
    • 2019-05-14
    • 2017-09-02
    • 1970-01-01
    • 2017-02-16
    • 2018-04-14
    相关资源
    最近更新 更多