Skip to content

Commit 3a21648

Browse files
JLHwungmathiasbynens
authored andcommitted
fix: declare the default export of the package index
`index.mjs` has a single default export, an object mapping each property type to its list of directories, but `index.d.mts` declared named exports instead, so `import dirs from '@unicode/unicode-X'` failed to type-check. The declaration now describes the default export with one `readonly <Type>: string[]` property per type. A test checks that the declared keys match the object exported at runtime. Assisted-By: Claude Fable 5.1 Issue: #109
1 parent 0d0dbaf commit 3a21648

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎index.mjs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,15 @@ const generateData = async (version) => {
328328
);
329329
fs.writeFileSync(
330330
path.resolve(__dirname, `output/unicode-${version}/index.d.mts`),
331-
Object.keys(dirMap)
332-
.map((key) => `export const ${key}: string[];`)
333-
.join('\n'),
331+
// `index.mjs` has a single default export: an object mapping each
332+
// property type to the list of its directories.
333+
[
334+
'declare const data: {',
335+
...Object.keys(dirMap).map((key) => `\treadonly ${key}: string[];`),
336+
'};',
337+
'export default data;',
338+
'',
339+
].join('\n'),
334340
);
335341
fs.writeFileSync(
336342
path.resolve(__dirname, `output/unicode-${version}/package.json`),

‎tests/tests.mjs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,16 @@ suite(`The generated latest Unicode js`, () => {
165165
}
166166
}
167167
});
168+
test('index.d.mts should declare the default export of index.mjs', async (t) => {
169+
const root = path.resolve(__dirname, '..', 'output', `unicode-${newest}`);
170+
const { default: data } = await import(path.join(root, 'index.mjs'));
171+
const declaration = fs.readFileSync(path.join(root, 'index.d.mts'), 'utf8');
172+
t.assert.match(declaration, /^export default data;$/m);
173+
const declaredKeys = [
174+
...declaration.matchAll(/^\treadonly (\w+): string\[\];$/gm),
175+
].map((m) => m[1]);
176+
t.assert.deepStrictEqual(declaredKeys, Object.keys(data));
177+
});
168178
test('type declarations should be shared via typesVersions', async (t) => {
169179
const root = path.resolve(__dirname, '..', 'output', `unicode-${newest}`);
170180
const pkg = JSON.parse(fs.readFileSync(path.join(root, 'package.json')));

0 commit comments

Comments
 (0)