refactor!: stricter Resolution type (#9582)

* refactor!: stricter `Resolution` type

* docs(changeset): don't leave it out

* fix: test

* fix: test

* refactor: replace `as` with `!`
This commit is contained in:
Khải
2025-05-30 14:17:12 +07:00
committed by GitHub
parent e49b782168
commit 6acf819c69
5 changed files with 15 additions and 14 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/resolver-base": major
"@pnpm/pick-fetcher": patch
---
Remove the blanket variant from the `Resolution` type, making it stricter and more useful.

View File

@@ -2,7 +2,7 @@ import type { Resolution } from '@pnpm/resolver-base'
import type { Fetchers, FetchFunction, DirectoryFetcher, GitFetcher } from '@pnpm/fetcher-base'
export function pickFetcher (fetcherByHostingType: Partial<Fetchers>, resolution: Resolution): FetchFunction | DirectoryFetcher | GitFetcher {
let fetcherType = resolution.type
let fetcherType: keyof Fetchers | undefined = resolution.type
if (resolution.type == null) {
if (resolution.tarball.startsWith('file:')) {
@@ -14,7 +14,7 @@ export function pickFetcher (fetcherByHostingType: Partial<Fetchers>, resolution
}
}
const fetch = fetcherByHostingType[fetcherType! as keyof Fetchers]
const fetch = fetcherByHostingType[fetcherType!]
if (!fetch) {
throw new Error(`Fetching for dependency type "${resolution.type ?? 'undefined'}" is not supported`)

View File

@@ -24,6 +24,6 @@ test.each([
test('should fail to pick fetcher if the type is not defined', () => {
expect(() => {
pickFetcher({}, { type: 'directory' })
pickFetcher({}, { type: 'directory', directory: expect.anything() })
}).toThrow('Fetching for dependency type "directory" is not supported')
})

View File

@@ -33,10 +33,9 @@ export interface GitResolution {
}
export type Resolution =
TarballResolution |
DirectoryResolution |
GitResolution |
({ type: string } & object)
| TarballResolution
| DirectoryResolution
| GitResolution
export interface ResolveResult {
id: PkgResolutionId

View File

@@ -1,5 +1,5 @@
import { type ResolveFunction } from '@pnpm/client'
import { type PkgResolutionId } from '@pnpm/resolver-base'
import { type PkgResolutionId, type TarballResolution } from '@pnpm/resolver-base'
import { getManifest } from '../lib/createManifestGetter'
test('getManifest()', async () => {
@@ -17,9 +17,7 @@ test('getManifest()', async () => {
name: 'foo',
version: '1.0.0',
},
resolution: {
type: 'tarball',
},
resolution: {} as TarballResolution,
resolvedVia: 'npm-registry',
}
}
@@ -37,9 +35,7 @@ test('getManifest()', async () => {
name: 'foo',
version: '2.0.0',
},
resolution: {
type: 'tarball',
},
resolution: {} as TarballResolution,
resolvedVia: 'npm-registry',
}
}