Skip to content

Commit 3123819

Browse files
authored
refactor: Fix TypeScript type correctness test (#2134)
1 parent 094f440 commit 3123819

28 files changed

+732
-629
lines changed

src/CoreManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ type ObjectController = {
5151
forceFetch: boolean,
5252
options: RequestOptions
5353
) => Promise<Array<ParseObject | undefined> | ParseObject | undefined>,
54-
save: (object: ParseObject | Array<ParseObject | ParseFile> | null, options: RequestOptions) => Promise<ParseObject | Array<ParseObject> | ParseFile>,
54+
save: (object: ParseObject | Array<ParseObject | ParseFile> | null, options: RequestOptions) => Promise<ParseObject | Array<ParseObject> | ParseFile | undefined>,
5555
destroy: (object: ParseObject | Array<ParseObject>, options: RequestOptions) => Promise<ParseObject | Array<ParseObject>>,
5656
};
5757
type ObjectStateController = {

src/ObjectStateMutations.js renamed to src/ObjectStateMutations.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
/**
2-
* @flow
3-
*/
4-
51
import encode from './encode';
62
import CoreManager from './CoreManager';
73
import ParseFile from './ParseFile';
84
import ParseRelation from './ParseRelation';
95
import TaskQueue from './TaskQueue';
106
import { RelationOp } from './ParseOp';
11-
127
import type { Op } from './ParseOp';
8+
import type ParseObject from './ParseObject';
139

1410
export type AttributeMap = { [attr: string]: any };
1511
export type OpsMap = { [attr: string]: Op };
@@ -43,7 +39,7 @@ export function setServerData(serverData: AttributeMap, attributes: AttributeMap
4339
}
4440
}
4541

46-
export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op: ?Op) {
42+
export function setPendingOp(pendingOps: Array<OpsMap>, attr: string, op?: Op) {
4743
const last = pendingOps.length - 1;
4844
if (op) {
4945
pendingOps[last][attr] = op;
@@ -84,7 +80,7 @@ export function estimateAttribute(
8480
pendingOps: Array<OpsMap>,
8581
object: ParseObject,
8682
attr: string
87-
): mixed {
83+
): any {
8884
let value = serverData[attr];
8985
for (let i = 0; i < pendingOps.length; i++) {
9086
if (pendingOps[i][attr]) {

src/Parse.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ import WebSocketController from './WebSocketController';
4343
* @global
4444
* @class
4545
* @hideconstructor
46-
*/
47-
46+
*/
4847
interface ParseType {
4948
ACL: typeof ACL,
5049
Parse?: ParseType,

src/ParseFile.js renamed to src/ParseFile.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
/**
2-
* @flow
3-
*/
41
/* global XMLHttpRequest, Blob */
52
import CoreManager from './CoreManager';
63
import type { FullOptions } from './RESTController';
7-
8-
const ParseError = require('./ParseError').default;
4+
import ParseError from './ParseError';
95

106
let XHR = null;
117
if (typeof XMLHttpRequest !== 'undefined') {
@@ -66,13 +62,13 @@ function b64Digit(number: number): string {
6662
*/
6763
class ParseFile {
6864
_name: string;
69-
_url: ?string;
65+
_url?: string;
7066
_source: FileSource;
71-
_previousSave: ?Promise<ParseFile>;
72-
_data: ?string;
73-
_requestTask: ?any;
74-
_metadata: ?Object;
75-
_tags: ?Object;
67+
_previousSave?: Promise<ParseFile>;
68+
_data?: string;
69+
_requestTask?: any;
70+
_metadata?: Object;
71+
_tags?: Object;
7672

7773
/**
7874
* @param name {String} The file's name. This will be prefixed by a unique
@@ -125,17 +121,17 @@ class ParseFile {
125121
file: data,
126122
type: specifiedType,
127123
};
128-
} else if (data && typeof data.uri === 'string' && data.uri !== undefined) {
124+
} else if (data && typeof (data as Uri).uri === 'string' && (data as Uri).uri !== undefined) {
129125
this._source = {
130126
format: 'uri',
131-
uri: data.uri,
127+
uri: (data as Uri).uri,
132128
type: specifiedType,
133129
};
134-
} else if (data && typeof data.base64 === 'string') {
135-
const base64 = data.base64.split(',').slice(-1)[0];
130+
} else if (data && typeof (data as Base64).base64 === 'string') {
131+
const base64 = (data as Base64).base64.split(',').slice(-1)[0];
136132
const dataType =
137133
specifiedType ||
138-
data.base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] ||
134+
(data as Base64).base64.split(';').slice(0, 1)[0].split(':').slice(1, 2)[0] ||
139135
'text/plain';
140136
this._data = base64;
141137
this._source = {
@@ -156,7 +152,7 @@ class ParseFile {
156152
*
157153
* @returns {Promise} Promise that is resolve with base64 data
158154
*/
159-
async getData(): Promise<String> {
155+
async getData(): Promise<string> {
160156
if (this._data) {
161157
return this._data;
162158
}
@@ -190,7 +186,7 @@ class ParseFile {
190186
* @param {object} options An object to specify url options
191187
* @returns {string | undefined}
192188
*/
193-
url(options?: { forceSecure?: boolean }): ?string {
189+
url(options?: { forceSecure?: boolean }): string | undefined {
194190
options = options || {};
195191
if (!this._url) {
196192
return;
@@ -207,7 +203,7 @@ class ParseFile {
207203
*
208204
* @returns {object}
209205
*/
210-
metadata(): Object {
206+
metadata() {
211207
return this._metadata;
212208
}
213209

@@ -216,7 +212,7 @@ class ParseFile {
216212
*
217213
* @returns {object}
218214
*/
219-
tags(): Object {
215+
tags() {
220216
return this._tags;
221217
}
222218

@@ -243,7 +239,7 @@ class ParseFile {
243239
* </ul>
244240
* @returns {Promise | undefined} Promise that is resolved when the save finishes.
245241
*/
246-
save(options?: FullOptions): ?Promise {
242+
save(options?: FileSaveOptions): Promise<ParseFile> | undefined {
247243
options = options || {};
248244
options.requestTask = task => (this._requestTask = task);
249245
options.metadata = this._metadata;
@@ -267,15 +263,15 @@ class ParseFile {
267263
return {};
268264
}
269265
const newSource = {
270-
format: 'base64',
266+
format: 'base64' as const,
271267
base64: result.base64,
272268
type: result.contentType,
273269
};
274270
this._data = result.base64;
275271
this._requestTask = null;
276272
return controller.saveBase64(this._name, newSource, options);
277273
})
278-
.then(res => {
274+
.then((res: { name?: string, url?: string }) => {
279275
this._name = res.name;
280276
this._url = res.url;
281277
this._requestTask = null;
@@ -317,7 +313,7 @@ class ParseFile {
317313
* <pre>
318314
* @returns {Promise} Promise that is resolved when the delete finishes.
319315
*/
320-
destroy(options?: FullOptions = {}) {
316+
destroy(options: FullOptions = {}) {
321317
if (!this._name) {
322318
throw new ParseError(ParseError.FILE_DELETE_UNNAMED_ERROR, 'Cannot delete an unnamed file.');
323319
}
@@ -333,15 +329,15 @@ class ParseFile {
333329
});
334330
}
335331

336-
toJSON(): { name: ?string, url: ?string } {
332+
toJSON(): { __type: 'File', name?: string, url?: string } {
337333
return {
338334
__type: 'File',
339335
name: this._name,
340336
url: this._url,
341337
};
342338
}
343339

344-
equals(other: mixed): boolean {
340+
equals(other: any): boolean {
345341
if (this === other) {
346342
return true;
347343
}
@@ -413,7 +409,7 @@ class ParseFile {
413409
return file;
414410
}
415411

416-
static encodeBase64(bytes: Array<number>): string {
412+
static encodeBase64(bytes: Array<number> | Uint8Array): string {
417413
const chunks = [];
418414
chunks.length = Math.ceil(bytes.length / 3);
419415
for (let i = 0; i < chunks.length; i++) {
@@ -441,10 +437,10 @@ const DefaultController = {
441437
if (source.format !== 'file') {
442438
throw new Error('saveFile can only be used with File-type sources.');
443439
}
444-
const base64Data = await new Promise((res, rej) => {
440+
const base64Data = await new Promise<string>((res, rej) => {
445441
// eslint-disable-next-line no-undef
446442
const reader = new FileReader();
447-
reader.onload = () => res(reader.result);
443+
reader.onload = () => res(reader.result as string);
448444
reader.onerror = error => rej(error);
449445
reader.readAsDataURL(source.file);
450446
});
@@ -455,9 +451,9 @@ const DefaultController = {
455451
// use the entire string instead
456452
const data = second ? second : first;
457453
const newSource = {
458-
format: 'base64',
454+
format: 'base64' as const,
459455
base64: data,
460-
type: source.type || (source.file ? source.file.type : null),
456+
type: source.type || (source.file ? source.file.type : undefined),
461457
};
462458
return await DefaultController.saveBase64(name, newSource, options);
463459
},
@@ -510,7 +506,7 @@ const DefaultController = {
510506
}
511507
},
512508

513-
downloadAjax: function (uri, options) {
509+
downloadAjax: function (uri: string, options: any) {
514510
return new Promise((resolve, reject) => {
515511
const xhr = new XHR();
516512
xhr.open('GET', uri, true);

src/ParseGeoPoint.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,15 @@ class ParseGeoPoint {
180180
* Creates a GeoPoint with the user's current location, if available.
181181
*
182182
* @param {object} options The options.
183-
* @param {boolean} [options.enableHighAccuracy=false] A boolean value that indicates the application would like to receive the best possible results. If true and if the device is able to provide a more accurate position, it will do so. Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example). On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
184-
* @param {number} [options.timeout=Infinity] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position. The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
185-
* @param {number} [options.maximumAge=0] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return. If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position. If set to Infinity the device must return a cached position regardless of its age. Default: 0.
183+
* @param {boolean} [options.enableHighAccuracy=false] A boolean value that indicates the application would like to receive the best possible results.
184+
* If true and if the device is able to provide a more accurate position, it will do so.
185+
* Note that this can result in slower response times or increased power consumption (with a GPS chip on a mobile device for example).
186+
* On the other hand, if false, the device can take the liberty to save resources by responding more quickly and/or using less power. Default: false.
187+
* @param {number} [options.timeout=Infinity] A positive long value representing the maximum length of time (in milliseconds) the device is allowed to take in order to return a position.
188+
* The default value is Infinity, meaning that getCurrentPosition() won't return until the position is available.
189+
* @param {number} [options.maximumAge=0] A positive long value indicating the maximum age in milliseconds of a possible cached position that is acceptable to return.
190+
* If set to 0, it means that the device cannot use a cached position and must attempt to retrieve the real current position.
191+
* If set to Infinity the device must return a cached position regardless of its age. Default: 0.
186192
* @static
187193
* @returns {Promise<Parse.GeoPoint>} User's current location
188194
*/

src/ParseHooks.js renamed to src/ParseHooks.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,52 @@ export function getTriggers() {
1313
return CoreManager.getHooksController().get('triggers');
1414
}
1515

16-
export function getFunction(name) {
16+
export function getFunction(name: string) {
1717
return CoreManager.getHooksController().get('functions', name);
1818
}
1919

20-
export function getTrigger(className, triggerName) {
20+
export function getTrigger(className: string, triggerName: string) {
2121
return CoreManager.getHooksController().get('triggers', className, triggerName);
2222
}
2323

24-
export function createFunction(functionName, url) {
24+
export function createFunction(functionName: string, url: string) {
2525
return create({ functionName: functionName, url: url });
2626
}
2727

28-
export function createTrigger(className, triggerName, url) {
28+
export function createTrigger(className: string, triggerName: string, url: string) {
2929
return create({ className: className, triggerName: triggerName, url: url });
3030
}
3131

32-
export function create(hook) {
32+
export function create(hook: HookDeclaration) {
3333
return CoreManager.getHooksController().create(hook);
3434
}
3535

36-
export function updateFunction(functionName, url) {
36+
export function updateFunction(functionName: string, url: string) {
3737
return update({ functionName: functionName, url: url });
3838
}
3939

40-
export function updateTrigger(className, triggerName, url) {
40+
export function updateTrigger(className: string, triggerName: string, url: string) {
4141
return update({ className: className, triggerName: triggerName, url: url });
4242
}
4343

44-
export function update(hook) {
44+
export function update(hook: HookDeclaration) {
4545
return CoreManager.getHooksController().update(hook);
4646
}
4747

48-
export function removeFunction(functionName) {
48+
export function removeFunction(functionName: string) {
4949
return remove({ functionName: functionName });
5050
}
5151

52-
export function removeTrigger(className, triggerName) {
52+
export function removeTrigger(className: string, triggerName: string) {
5353
return remove({ className: className, triggerName: triggerName });
5454
}
5555

56-
export function remove(hook) {
56+
export function remove(hook: HookDeleteArg) {
5757
return CoreManager.getHooksController().remove(hook);
5858
}
5959

6060
const DefaultController = {
61-
get(type, functionName, triggerName) {
61+
get(type: string, functionName?: string, triggerName?: string) {
6262
let url = '/hooks/' + type;
6363
if (functionName) {
6464
url += '/' + functionName;
@@ -111,7 +111,7 @@ const DefaultController = {
111111
return this.sendRequest('PUT', url, hook);
112112
},
113113

114-
sendRequest(method, url, body) {
114+
sendRequest(method: string, url: string, body?: any) {
115115
return CoreManager.getRESTController()
116116
.request(method, url, body, { useMasterKey: true })
117117
.then(res => {

0 commit comments

Comments
 (0)