Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable noUncheckedIndexedAccess in tsconfig.json #829

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/camera/core-impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,12 @@ class RenderedCameraImpl implements RenderedCamera {
private getFirstTrackOrFail(): MediaStreamTrack {
this.failIfClosed();

if (this.mediaStream.getVideoTracks().length === 0) {
const firstTrack = this.mediaStream.getVideoTracks()[0];
if (!firstTrack) {
throw "No video tracks found";
}

return this.mediaStream.getVideoTracks()[0];
}
return firstTrack;
}

//#region Public APIs.
Expand Down
2 changes: 1 addition & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Html5QrcodeConstants {
static DEFAULT_REMEMBER_LAST_CAMERA_USED = true;
static DEFAULT_SUPPORTED_SCAN_TYPE = [
Html5QrcodeScanType.SCAN_TYPE_CAMERA,
Html5QrcodeScanType.SCAN_TYPE_FILE];
Html5QrcodeScanType.SCAN_TYPE_FILE] as const;
}

/** Defines dimension for QR Code Scanner. */
Expand Down
2 changes: 1 addition & 1 deletion src/html5-qrcode-scanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export interface Html5QrcodeScannerConfig
* - [SCAN_TYPE_FILE] - Only file based scan supported.
* - Setting wrong values or multiple values will fail.
*/
supportedScanTypes?: Array<Html5QrcodeScanType> | [];
supportedScanTypes?: ReadonlyArray<Html5QrcodeScanType> | [];

/**
* If `true` the rendered UI will have button to turn flash on or off
Expand Down
4 changes: 2 additions & 2 deletions src/html5-qrcode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1259,12 +1259,12 @@ export class Html5Qrcode {
};

const keys = Object.keys(cameraIdOrConfig);
if (keys.length !== 1) {
const key = keys[0];
if (!key) {
throw "'cameraIdOrConfig' object should have exactly 1 key,"
+ ` if passed as an object, found ${keys.length} keys`;
}

const key:string = Object.keys(cameraIdOrConfig)[0];
if (key !== facingModeKey && key !== deviceIdKey) {
throw `Only '${facingModeKey}' and '${deviceIdKey}' `
+ " are supported for 'cameraIdOrConfig'";
Expand Down
5 changes: 2 additions & 3 deletions src/ui/scanner/file-selection-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,10 @@ export class FileSelectionUi {
return;
}
let target: HTMLInputElement = e.target as HTMLInputElement;
if (target.files && target.files.length === 0) {
const file = target.files?.[0];
if (!file) {
return;
}
let fileList: FileList = target.files!;
const file: File = fileList[0];
let fileName = file.name;
$this.setImageNameToButton(fileName);

Expand Down
10 changes: 5 additions & 5 deletions src/ui/scanner/scan-type-selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {

/** Util class to help with scan type selection in scanner class. */
export class ScanTypeSelector {
private supportedScanTypes: Array<Html5QrcodeScanType>;
private supportedScanTypes: readonly [Html5QrcodeScanType, ...Array<Html5QrcodeScanType>];

constructor(supportedScanTypes?: Array<Html5QrcodeScanType> | []) {
constructor(supportedScanTypes?: ReadonlyArray<Html5QrcodeScanType> | []) {
this.supportedScanTypes = this.validateAndReturnScanTypes(
supportedScanTypes);
}
Expand Down Expand Up @@ -65,8 +65,8 @@ export class ScanTypeSelector {
* Fails early if the config values is incorrectly set.
*/
private validateAndReturnScanTypes(
supportedScanTypes?:Array<Html5QrcodeScanType>):
Array<Html5QrcodeScanType> {
supportedScanTypes?:ReadonlyArray<Html5QrcodeScanType>):
readonly [Html5QrcodeScanType, ...Array<Html5QrcodeScanType>] {
// If not set, use the default values and order.
if (!supportedScanTypes || supportedScanTypes.length === 0) {
return Html5QrcodeConstants.DEFAULT_SUPPORTED_SCAN_TYPE;
Expand All @@ -88,7 +88,7 @@ export class ScanTypeSelector {
}
}

return supportedScanTypes;
return supportedScanTypes as [Html5QrcodeScanType, ...Array<Html5QrcodeScanType>];
}
//#endregion
}
8 changes: 4 additions & 4 deletions tests/ui/scanner/camera-selection-ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ describe("CameraSelectionUi setting and getting values", () => {
let cameraSelectUi = CameraSelectionUi.create(parentElement!, cameras);

// First camera is default.
expect(cameraSelectUi.getValue()).eq(cameras[0].id);
expect(cameraSelectUi.getValue()).eq(cameras[0]!.id);

cameraSelectUi.setValue(cameras[1].id);
expect(cameraSelectUi.getValue()).eq(cameras[1].id);
cameraSelectUi.setValue(cameras[1]!.id);
expect(cameraSelectUi.getValue()).eq(cameras[1]!.id);

expect(() => {
cameraSelectUi.setValue("random string");
Expand All @@ -138,7 +138,7 @@ describe("CameraSelectionUi setting and getting values", () => {
let cameras = createCameraList(numCameras);
let cameraSelectUi = CameraSelectionUi.create(parentElement!, cameras);

expect(cameraSelectUi.hasValue(cameras[1].id)).to.be.true;
expect(cameraSelectUi.hasValue(cameras[1]!.id)).to.be.true;
expect(cameraSelectUi.hasValue("random string")).to.be.false;
});
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// Flags to ensure code quality.
"strict": true,
"noImplicitReturns": true,
"noUncheckedIndexedAccess": true,
"noUnusedParameters": true,
"experimentalDecorators": true,
"removeComments": true,
Expand Down