对于 TypeScript 语言用户
使用 expo-cli 36 在基于 javascript 的 react-native 项目上进行测试,并在使用 Angular 7 时编写
我有错误将我带到这里......我使用的是基于 js 的 react-native。之前我写了一个 JS 库,后来因为一些需要,我把它改成了 TS,在切换到 Angular 之后,我不得不用它创建一个坚固的库才能在那个环境中工作......
不,我在这里复制了这些文件,我遇到了错误,我来到这个线程,但是一旦我第一次编译它,所有错误就消失了,并且它完全有效......
这是我如何使用它:
我有实用程序类Utility.ts
:
export class Utility {
/**
* Returns False If Incoming Variable Is Not Null, void or Undefined, Otherwise True
*/
public static isNullOrUndefined(obj: any|void): boolean {
// return obj == null // juggling-check
return typeof obj === 'undefined' || obj === null; // strict-check
}
/**
* Returns False If Incoming Variable Does Not Contains Data, Otherwise True
*/
public static isNullOrUndefinedOrEmpty(obj: any|void): boolean {
if (Utility.isNullOrUndefined(obj)) {
return true;
}
// typeof for primitive string, instanceof for objective string
if (typeof(obj) === 'string' || obj instanceof String) {
return (obj as string).valueOf() === '';
} else if (obj instanceof Array) {
return (obj as Array<any>).length === 0;
}
throw new Error('Not Supported Exception');
}
...
}
我有一个定义类index.d.ts
(我不确定,但我认为文件名在 Angular 时间非常重要,请随意测试...):
declare global {
interface StringConstructor { // for static methods
format: (str: string, ...args: any[]) => string;
}
interface String { // for instance methods
/**
* Support StringComparision Options
* https://stackoverflow.com/questions/10636871/ordinal-string-compare-in-javascript
* @param searchString {!string}
* @param position {?number}
* @param comparision {?StringComparison}
* @returns {number}
*/
indexOfString: (searchString: string, position?: number, comparision?: StringComparison) => number;
insertAt: (index: number, strText: string) => string;
removeAt: (index: number, count: number) => string;
replaceAt: (index: number, count: number, strReplacement: string) => string;
overrideAt: (index: number, strText: string) => string;
...
}
最后我有我的扩展文件Extensions.ts
:
import { Utility } from './Utility';
/**
* Support StringComparision Options
* https://stackoverflow.com/questions/10636871/ordinal-string-compare-in-javascript
*/
String.prototype.indexOfString = function(searchString: string, position?: number, comparision?: StringComparison): number {
return Utility.indexOfString(this, searchString, position, comparision);
};
String.prototype.insertAt = function(index: number, strText: string): string {
return this.substr(0, index) + strText + this.substr(index);
};
String.prototype.removeAt = function(index: number, count: number): string {
if (Utility.isNullOrUndefined(count)) {
count = this.length - index;
}
return this.substr(0, index) + this.substr(index + count);
};
在这里,我将所有这些文件放在一个名为的文件夹中,该文件夹util
根本无关紧要,并且我可以从扩展直接访问实用程序,反之亦然(或者直到现在我没有任何问题。
现在虽然我仍然无法在 react 组件中使用我的扩展,但如果我添加一个简单的导入,我可以:import '../util/Extensions';
我是这样测试的:
import React from 'react';
import { Text, View } from 'react-native';
import '../util/Extensions'; //my import
const SurveyScreen = props => {
const z = 'sdwqew';
const x = z.insertAt(2,'hassan'); //function from my custom extensions
return (
<View>
<Text>Hello World</Text>
<Text>{x}</Text>
</View>
);
}
export default SurveyScreen;
请注意,此时我没有足够的时间来全面概述我的旧代码,但事情就是这样
第二个注意事项:如果我在实用程序中导入扩展,它会给我警告要求循环被允许,但可能导致未初始化的值。考虑重构以消除对循环的需要。
如果我没有在 VSCode 上看到一些错误,但它编译得很好,没有警告......