Applied changes to solve several issues about typescript strict mode

This commit is contained in:
Alexander Cerutti
2021-12-23 19:46:29 +01:00
parent ef20bc5a44
commit cf8a467266
6 changed files with 56 additions and 38 deletions

View File

@@ -25,7 +25,7 @@ export default class PKPass extends Bundle {
[placeholder: string]: string;
};
} = {};
private [passTypeSymbol]: Schemas.PassTypesProps = undefined;
private [passTypeSymbol]: Schemas.PassTypesProps | undefined = undefined;
/**
* Either create a pass from another one
@@ -39,8 +39,8 @@ export default class PKPass extends Bundle {
source: S,
props?: Schemas.OverridablePassProps,
): Promise<PKPass> {
let certificates: Schemas.CertificatesSchema = undefined;
let buffers: Schemas.FileBuffers = undefined;
let certificates: Schemas.CertificatesSchema | undefined = undefined;
let buffers: Schemas.FileBuffers | undefined = undefined;
if (!source) {
throw new TypeError(
@@ -206,8 +206,10 @@ export default class PKPass extends Bundle {
/**
* Allows setting a transitType property
* for a boardingPass. Throws an error if
* the current type is not a boardingPass.
* for a boardingPass.
*
* It will (automatically) throw an exception
* if current type is not "boardingPass".
*
* @param value
*/
@@ -224,12 +226,16 @@ export default class PKPass extends Bundle {
value,
Messages.TRANSIT_TYPE.INVALID,
);
this[propsSymbol]["boardingPass"].transitType = value;
}
/**
* Allows getting the current transitType
* from pass props
* from pass props.
*
* It will (automatically) throw an exception
* if current type is not "boardingPass".
*/
public get transitType() {
@@ -239,9 +245,9 @@ export default class PKPass extends Bundle {
/**
* Allows accessing to primaryFields object.
*
* It will (automatically) throw an error if
* no valid pass.json has been parsed yet or,
* anyway, if it has not a valid type.
* It will (automatically) throw an exception
* if no valid pass.json has been parsed yet
* or, anyway, if it has not a valid type.
*/
public get primaryFields(): Schemas.Field[] {
@@ -251,9 +257,10 @@ export default class PKPass extends Bundle {
/**
* Allows accessing to secondaryFields object
*
* It will (automatically) throw an error if
* no valid pass.json has been parsed yet or,
* anyway, if it has not a valid type.
* It will (automatically) throw an exception
* if no valid pass.json has been parsed yet
* or, anyway, if a valid type has not been
* set yet.
*/
public get secondaryFields(): Schemas.Field[] {
@@ -263,9 +270,10 @@ export default class PKPass extends Bundle {
/**
* Allows accessing to auxiliaryFields object
*
* It will (automatically) throw an error if
* no valid pass.json has been parsed yet or,
* anyway, if it has not a valid type.
* It will (automatically) throw an exception
* if no valid pass.json has been parsed yet
* or, anyway, if a valid type has not been
* set yet.
*/
public get auxiliaryFields(): Schemas.Field[] {
@@ -275,9 +283,10 @@ export default class PKPass extends Bundle {
/**
* Allows accessing to headerFields object
*
* It will (automatically) throw an error if
* no valid pass.json has been parsed yet or,
* anyway, if it has not a valid type.
* It will (automatically) throw an exception
* if no valid pass.json has been parsed yet
* or, anyway, if a valid type has not been
* set yet.
*/
public get headerFields(): Schemas.Field[] {
@@ -287,9 +296,10 @@ export default class PKPass extends Bundle {
/**
* Allows accessing to backFields object
*
* It will (automatically) throw an error if
* no valid pass.json has been parsed yet or,
* anyway, if it has not a valid type.
* It will (automatically) throw an exception
* if no valid pass.json has been parsed yet
* or, anyway, if a valid type has not been
* set yet.
*/
public get backFields(): Schemas.Field[] {
@@ -305,15 +315,18 @@ export default class PKPass extends Bundle {
* headerFields, auxiliaryFields, backFields)
*/
public set type(type: Schemas.PassTypesProps) {
public set type(maybeNewType: Schemas.PassTypesProps | undefined) {
Utils.assertUnfrozen(this);
Schemas.assertValidity(
Schemas.PassType,
type,
maybeNewType,
Messages.PASS_TYPE.INVALID,
);
/** Shut up, typescript strict mode! */
const type = maybeNewType as Schemas.PassTypesProps;
if (this.type) {
/**
* Removing reference to previous type and its content because
@@ -431,7 +444,7 @@ export default class PKPass extends Bundle {
const translationsFileRegexp =
/(?<lang>[a-zA-Z-]{2,}).lproj\/pass\.strings/;
let match: RegExpMatchArray;
let match: RegExpMatchArray | null;
if ((match = pathName.match(translationsFileRegexp))) {
const [, lang] = match;
@@ -501,7 +514,7 @@ export default class PKPass extends Bundle {
secondaryFields = [],
auxiliaryFields = [],
backFields = [],
} = data[type];
} = data[type] || {};
this.headerFields.push(...headerFields);
this.primaryFields.push(...primaryFields);
@@ -810,7 +823,7 @@ export default class PKPass extends Bundle {
this[propsSymbol]["beacons"] = Schemas.filterValid(
Schemas.Beacon,
beacons,
beacons as Schemas.Beacon[],
);
}
@@ -846,7 +859,7 @@ export default class PKPass extends Bundle {
this[propsSymbol]["locations"] = Schemas.filterValid(
Schemas.Location,
locations,
locations as Schemas.Location[],
);
}
@@ -958,8 +971,8 @@ export default class PKPass extends Bundle {
function validateJSONBuffer(
buffer: Buffer,
schema: Parameters<typeof Schemas.validate>[0],
) {
let contentAsJSON: unknown;
): Schemas.PassProps {
let contentAsJSON: Schemas.PassProps;
try {
contentAsJSON = JSON.parse(buffer.toString("utf8"));

View File

@@ -116,7 +116,10 @@ function parseCertificates(certificates: Schemas.CertificatesSchema) {
function getStringCertificates(
certificates: Schemas.CertificatesSchema,
): Record<keyof Schemas.CertificatesSchema, string> {
): Record<
keyof Omit<Schemas.CertificatesSchema, "signerKeyPassphrase">,
string
> & { signerKeyPassphrase?: string } {
return {
signerKeyPassphrase: certificates.signerKeyPassphrase,
wwdr: Buffer.from(certificates.wwdr).toString("utf-8"),

View File

@@ -30,14 +30,14 @@ export function parse(buffer: Buffer) {
/** EOF */
blockEndPoint === fileAsString.length
) {
let match: RegExpMatchArray;
let match: RegExpMatchArray | null;
const section = fileAsString.substring(
blockStartPoint,
blockEndPoint + 1,
);
if ((match = section.match(translationRowRegex))) {
if ((match = section.match(translationRowRegex)) && match.groups) {
const {
groups: { key, value },
} = match;

View File

@@ -49,7 +49,9 @@ export default async function getModelFolderContents(
.reduce((acc, current) => ({ ...acc, ...current }), {});
return modelRecords;
} catch (err) {
} catch (_err) {
const err = _err as NodeJS.ErrnoException;
if (err?.code === "ENOENT") {
if (err.syscall === "open") {
// file opening failed

View File

@@ -229,7 +229,7 @@ export function filterValid<T extends Object>(
return [];
}
return source.reduce((acc, current) => {
return source.reduce<T[]>((acc, current) => {
try {
return [...acc, validate(schema, current)];
} catch (err) {

View File

@@ -7,7 +7,7 @@ import type Bundle from "./Bundle";
* @returns
*/
export function processDate(date: Date): string | null {
export function processDate(date: Date): string | undefined {
if (!(date instanceof Date)) {
throw "Invalid date";
}
@@ -84,9 +84,9 @@ export function removeHidden(from: Array<string>): Array<string> {
* @returns
*/
export function cloneRecursive(object: Object) {
const objectCopy = {};
const objectEntries = Object.entries(object);
export function cloneRecursive<T extends Object>(object: T) {
const objectCopy = {} as Record<keyof T, any>;
const objectEntries = Object.entries(object) as [keyof T, T[keyof T]][];
for (let i = 0; i < objectEntries.length; i++) {
const [key, value] = objectEntries[i];