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

View File

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

View File

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

View File

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

View File

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

View File

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