packager icon indicating copy to clipboard operation
packager copied to clipboard

Better types for TargetArch and TargetPlatform

Open RareScrap opened this issue 8 months ago • 1 comments

Preflight Checklist

  • [x] I have read the contribution documentation for this project.
  • [x] I agree to follow the code of conduct that this project follows, as appropriate.
  • [x] I have searched the issue tracker for a feature request that matches the one I want to file, without success.

Problem Description

TypeScript compiler can't validate string literals for TargetArch and TargetPlatform types:

import { HookFunction, HookFunctionErrorCallback, OfficialArch, OfficialPlatform, TargetArch, TargetPlatform } from '@electron/packager';
// ...

const hookFunctionExample: HookFunction = (buildPath: string, electronVersion: string, platform: TargetPlatform, arch: TargetArch, callback: HookFunctionErrorCallback) => {
    const something1: TargetArch = "foo" // will compile, but obviously incorrect
    const something2: TargetPlatform = "Шind0wz 123" // will compile, but obviously incorrect

    const something3: OfficialArch = "foo" // will not compile, but distincts from the `arch` parameter's type
    const something4: OfficialPlatform = "Шind0wz 123" // will not compile, but distincts from the `platform` parameter's type

    // ...
}

// ...

That's because the TypeScript compiler expands these types to any value string (source link):

// From now TargetArch and TargetPlatform are basically unconstrained string type aliases
export type TargetArch = OfficialArch | string;
export type TargetPlatform = OfficialPlatform | string;

Proposed Solution

I see several possible solutions:

  1. Remove TargetArch and TargetPlatform in flavor of OfficialArch and OfficialPlatform respectively
    1. Or at least do it in HookFunction signature
  2. Remove | string type expansion from TargetArch and TargetPlatform

I'm not sure what is the semantic difference between OfficialArch and TargetArch types so a clarification from Electron Packager devs is required to choose the best solution

Alternatives Considered

None

Additional Information

None

RareScrap avatar Mar 13 '25 08:03 RareScrap

👋 Thanks for opening your first issue here! If you have a question about using Electron Packager, read the support docs. If you're reporting a 🐞 bug, please make sure you include steps to reproduce it. Development and issue triage is community-driven, so please be patient and we will get back to you as soon as we can.

To help make it easier for us to investigate your issue, please follow the contributing guidelines.

welcome[bot] avatar Mar 13 '25 08:03 welcome[bot]