Переопределить функцию-прототип строки, не компилирующуюся в угловой библиотеке - PullRequest
1 голос
/ 18 апреля 2019

строка-prototype.service.ts

String.prototype.equals = equals;
String.prototype.includes = includes;

interface String {
  equals: typeof equals;
  includes: typeof includes;
}

function equals(a):boolean {
  if(typeof (a) !== "string"){
    a = a.toString();
  }
  return this.toLowerCase() == a.toLowerCase();
}

function includes(searchString:string, position?:number):boolean {
  let stringS:any = searchString;
  if(typeof (stringS) !== "string"){
    searchString = stringS.toString();
  }
  let k = this.toLowerCase().match(searchString.toLowerCase());
  if (position && k) {
    return k.index === position;
  }
  return !!k;
}

Теперь я импортирую файл TS в модуль библиотеки угловая -. **** module.ts

import { DataTableComponent } from './data-table/data-table.component';
import {DragDropModule} from '@angular/cdk/drag-drop';
import { ClickOutsideModule } from 'ng4-click-outside';
import './string-prototype.service.ts';

Теперь я использовал функцию-прототип строки в data-table.component , в моем угловом приложении она работает нормально. Но после того, как я преобразовал в угловую библиотеку и когда я строю библиотеку, я получил ошибку ниже:

BUILD ERROR
projects/angular-***-***/src/lib/data-table/data-table.component.ts(296,75): error TS2339: Property 'equals' does not exist on type 'String'.

Error: projects/angular-***-***/src/lib/data-table/data-table.component.ts(296,75): error TS2339: Property 'equals' does not exist on type 'String'.

    at Object.<anonymous> (D:\Projects\Angular 7\Library\angular-***-***-lib\node_modules\ng-packagr\lib\ngc\compile-source-files.js:65:19)
    at Generator.next (<anonymous>)
    at fulfilled (D:\Projects\Angular 7\Library\angular-***-***-lib\node_modules\ng-packagr\lib\ngc\compile-source-files.js:4:58)

Как устранить ошибку. Странно то, что приложение работает нормально. Но в случае с Lib это не компилируется.

Заранее спасибо.

...