Skip to content

Better types for TargetArch and TargetPlatform #1796

@RareScrap

Description

@RareScrap

Preflight Checklist

  • I have read the contribution documentation for this project.
  • I agree to follow the code of conduct that this project follows, as appropriate.
  • 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions