diff --git a/src/any-map.ts b/src/any-map.ts index 8b374ee..7138d73 100644 --- a/src/any-map.ts +++ b/src/any-map.ts @@ -12,6 +12,7 @@ import type { SectionedSourceMap, DecodedSourceMap, SectionedSourceMapInput, + Ro, } from './types'; import type { SourceMapSegment } from './sourcemap-segment'; @@ -46,7 +47,7 @@ export const AnyMap: AnyMap = function (map, mapUrl) { } as AnyMap; function recurse( - input: SectionedSourceMap, + input: Ro, mapUrl: string | null | undefined, mappings: SourceMapSegment[][], sources: string[], @@ -90,7 +91,7 @@ function recurse( } function addSection( - input: Section['map'], + input: Ro, mapUrl: string | null | undefined, mappings: SourceMapSegment[][], sources: string[], diff --git a/src/trace-mapping.ts b/src/trace-mapping.ts index 77c7fbf..4698e35 100644 --- a/src/trace-mapping.ts +++ b/src/trace-mapping.ts @@ -159,7 +159,7 @@ export class TraceMap implements SourceMap { if (!isString && (map as unknown as { _decodedMemo: any })._decodedMemo) return map as TraceMap; - const parsed = (isString ? JSON.parse(map) : map) as Exclude; + const parsed = (isString ? JSON.parse(map) : map) as DecodedSourceMap | EncodedSourceMap; const { version, file, names, sourceRoot, sources, sourcesContent } = parsed; this.version = version; diff --git a/src/types.ts b/src/types.ts index 964e290..b62f7c5 100644 --- a/src/types.ts +++ b/src/types.ts @@ -57,8 +57,9 @@ export type InvalidGeneratedMapping = { export type Bias = typeof GREATEST_LOWER_BOUND | typeof LEAST_UPPER_BOUND; -export type SourceMapInput = string | EncodedSourceMap | DecodedSourceMap | TraceMap; -export type SectionedSourceMapInput = SourceMapInput | SectionedSourceMap; +export type SourceMapInput = string | Ro | Ro | TraceMap; + +export type SectionedSourceMapInput = SourceMapInput | Ro; export type Needle = { line: number; column: number; bias?: Bias }; export type SourceNeedle = { source: string; line: number; column: number; bias?: Bias }; @@ -90,3 +91,11 @@ export abstract class SourceMap { declare sourcesContent: SourceMapV3['sourcesContent']; declare resolvedSources: SourceMapV3['sources']; } + +export type Ro = T extends Array + ? V[] | Readonly | RoArray | Readonly> + : T extends object + ? T | Readonly | RoObject | Readonly> + : T; +type RoArray = Ro[]; +type RoObject = { [K in keyof T]: T[K] | Ro }; diff --git a/test/any-map.test.ts b/test/any-map.test.ts index b9ba1ff..2169115 100644 --- a/test/any-map.test.ts +++ b/test/any-map.test.ts @@ -3,7 +3,7 @@ import { test, describe } from './setup'; import { AnyMap, encodedMappings, decodedMappings } from '../src/trace-mapping'; -import type { SectionedSourceMap } from '../src/trace-mapping'; +import type { SectionedSourceMap, SourceMapSegment } from '../src/trace-mapping'; describe('AnyMap', () => { const map: SectionedSourceMap = { @@ -126,4 +126,21 @@ describe('AnyMap', () => { ]); }); }); + + describe('typescript readonly type', () => { + test('decoded source map', (t) => { + // This is a TS lint test, not a real one. + t.pass(); + + const decodedMap = { + version: 3 as const, + sources: ['input.js'] as readonly string[], + names: [] as readonly string[], + mappings: [] as readonly SourceMapSegment[][], + sourcesContent: [] as readonly string[], + }; + + new AnyMap(decodedMap); + }); + }); }); diff --git a/test/trace-mapping.test.ts b/test/trace-mapping.test.ts index ab0da2a..b740908 100644 --- a/test/trace-mapping.test.ts +++ b/test/trace-mapping.test.ts @@ -24,6 +24,7 @@ import type { EncodedSourceMap, DecodedSourceMap, EachMapping, + SourceMapSegment, } from '../src/trace-mapping'; describe('TraceMap', () => { @@ -462,4 +463,21 @@ describe('TraceMap', () => { t.is(encodedMappings(tracer), ''); }); }); + + describe('typescript readonly type', () => { + test('decoded source map', (t) => { + // This is a TS lint test, not a real one. + t.pass(); + + const decodedMap = { + version: 3 as const, + sources: ['input.js'] as readonly string[], + names: [] as readonly string[], + mappings: [] as readonly SourceMapSegment[][], + sourcesContent: [] as readonly string[], + }; + + new TraceMap(decodedMap); + }); + }); });