检查打字稿中是否存在函数

时间:2017-03-27 10:54:13

标签: angular typescript ionic2

我正在将js代码转换为typescript(angular 1到2)......痛苦的操作!

if (_usersDatabase.updateReplication == null) {下方的行会导致打字稿错误:

Typescript Error
Property 'updateReplication' does not exist on type 'Database<{}>'.

。它是为了检查函数是否已经定义。

我怎么能为打字稿写这个?

import { MigrationService } from '../providers/migration-service';
import { CouchConstants } from '../couch-constants';
import * as PouchDB from 'pouchdb';

@Injectable()
export class UsersDatabase {

    constructor(
        private storageService: LocalStorageService
        , private UtilsService: UtilsService
        , private MigrationService: MigrationService

    ) {

    'use strict';

    var _usersDatabase = new PouchDB(CouchConstants.COUCHDB_USERS_DB_NAME);

    if (_usersDatabase.updateReplication == null) {

        _usersDatabase.updateReplication = function (newDocsIds) {

1 个答案:

答案 0 :(得分:5)

  • 无需在打字稿文件
  • 中使用'use strict';
  • 使用this访问他们的方法和变量。

    @Injectable()
    export class UsersDatabase {
    private _usersDatabase : any;
    
    constructor(
        private storageService: LocalStorageService
        , private UtilsService: UtilsService
        , private MigrationService: MigrationService
    
    ) {
    
    this._usersDatabase = new PouchDB(CouchConstants.COUCHDB_USERS_DB_NAME);
    
    if (this._usersDatabase.updateReplication == null) {
    
            this._usersDatabase.updateReplication = function (newDocsIds) {
    
            }
     }
    }