diff --git a/src/@types/pkg.ts b/src/@types/pkg.ts index 026eb5080..d42ba6961 100644 --- a/src/@types/pkg.ts +++ b/src/@types/pkg.ts @@ -20,10 +20,14 @@ export interface GithubRepo { head: string; } -export interface TsType { - ts: 'definitely-typed' | 'included' | false; - definitelyTyped: string | null; -} +export type TsType = + | { + ts: 'included' | false | { possible: true; dtsMain: string }; + } + | { + ts: 'definitely-typed'; + definitelyTyped: string; + }; export type ModuleType = 'esm' | 'cjs' | 'unknown'; diff --git a/src/__tests__/typescript.test.js b/src/__tests__/typescript.test.ts similarity index 85% rename from src/__tests__/typescript.test.js rename to src/__tests__/typescript.test.ts index 98f05d688..42e05f811 100644 --- a/src/__tests__/typescript.test.js +++ b/src/__tests__/typescript.test.ts @@ -1,5 +1,5 @@ import * as npm from '../npm'; -import { getTypeScriptSupport } from '../typescriptSupport.js'; +import { getTypeScriptSupport } from '../typescriptSupport'; import { fileExistsInUnpkg } from '../unpkg'; jest.mock('../npm'); @@ -10,6 +10,7 @@ describe('getTypeScriptSupport()', () => { const typesSupport = await getTypeScriptSupport({ name: 'Has Types', types: { ts: 'included' }, + version: '1.0.0', }); expect(typesSupport).toEqual({ types: { ts: 'included' } }); @@ -17,10 +18,12 @@ describe('getTypeScriptSupport()', () => { describe('without types/typings', () => { it('Checks for @types/[name]', async () => { + // @ts-expect-error npm.validatePackageExists.mockResolvedValue(true); const atTypesSupport = await getTypeScriptSupport({ name: 'my-lib', types: { ts: false }, + version: '1.0.0', }); expect(atTypesSupport).toEqual({ types: { @@ -31,10 +34,12 @@ describe('getTypeScriptSupport()', () => { }); it('Checks for @types/[scope__name]', async () => { + // @ts-expect-error npm.validatePackageExists.mockResolvedValue(true); const atTypesSupport = await getTypeScriptSupport({ name: '@my-scope/my-lib', types: { ts: false }, + version: '1.0.0', }); expect(atTypesSupport).toEqual({ types: { @@ -45,23 +50,29 @@ describe('getTypeScriptSupport()', () => { }); it('Checks for a d.ts resolved version of main', async () => { + // @ts-expect-error npm.validatePackageExists.mockResolvedValue(false); + // @ts-expect-error fileExistsInUnpkg.mockResolvedValue(true); const typesSupport = await getTypeScriptSupport({ name: 'my-lib', types: { ts: { possible: true, dtsMain: 'main.d.ts' } }, + version: '1.0.0', }); expect(typesSupport).toEqual({ types: { ts: 'included' } }); }); it('Handles not having any possible TS types', async () => { + // @ts-expect-error npm.validatePackageExists.mockResolvedValue(false); + // @ts-expect-error fileExistsInUnpkg.mockResolvedValue(false); const typesSupport = await getTypeScriptSupport({ name: 'my-lib', types: { ts: false }, + version: '1.0.0', }); expect(typesSupport).toEqual({ types: { ts: false } }); }); diff --git a/src/saveDocs.js b/src/saveDocs.js index 39008084a..4e3b6a7a0 100644 --- a/src/saveDocs.js +++ b/src/saveDocs.js @@ -2,7 +2,7 @@ import { getChangelogs } from './changelog'; import formatPkg from './formatPkg.js'; import * as jsDelivr from './jsDelivr'; import * as npm from './npm'; -import { getTSSupport } from './typescriptSupport.js'; +import { getTSSupport } from './typescriptSupport'; import { datadog } from './utils/datadog'; import { log } from './utils/log'; diff --git a/src/typescriptSupport.js b/src/typescriptSupport.ts similarity index 80% rename from src/typescriptSupport.js rename to src/typescriptSupport.ts index f82b29db2..587b8064b 100644 --- a/src/typescriptSupport.js +++ b/src/typescriptSupport.ts @@ -1,3 +1,4 @@ +import type { RawPkg } from './@types/pkg'; import * as npm from './npm'; import { fileExistsInUnpkg } from './unpkg'; import { datadog } from './utils/datadog'; @@ -9,7 +10,9 @@ import { datadog } from './utils/datadog'; * - { types: { ts: "included" }} - for types shipped with the module. * */ -export async function getTypeScriptSupport(pkg) { +export async function getTypeScriptSupport( + pkg: Pick +): Promise> { // Already calculated in `formatPkg` if (typeof pkg.types.ts === 'string') { return { types: pkg.types }; @@ -47,9 +50,11 @@ export async function getTypeScriptSupport(pkg) { } /** - * @param {Array} pkgs + * @param pkgs */ -export async function getTSSupport(pkgs) { +export async function getTSSupport( + pkgs: Array> +): Promise>> { const start = Date.now(); const all = await Promise.all(pkgs.map(getTypeScriptSupport));