MIRROR: javascript for ๐Ÿœ's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

typescript type only import elision

+64 -1
+1 -1
src/strip/src/strip.rs
··· 28 28 let scoping = semantic_ret.semantic.into_scoping(); 29 29 let transform_options = TransformOptions { 30 30 typescript: TypeScriptOptions { 31 - only_remove_type_imports: true, 31 + only_remove_type_imports: false, 32 32 ..Default::default() 33 33 }, 34 34 ..Default::default()
+63
tests/test_typescript_type_only_import_elision.cjs
··· 1 + const { spawnSync } = require('child_process'); 2 + const fs = require('fs'); 3 + const os = require('os'); 4 + const path = require('path'); 5 + 6 + function assert(condition, message) { 7 + if (!condition) throw new Error(message); 8 + } 9 + 10 + const tmpRoot = fs.mkdtempSync(path.join(os.tmpdir(), 'ant-ts-type-import-')); 11 + const packageRoot = path.join(tmpRoot, 'node_modules', '@acme', 'ts-type-imports'); 12 + const scriptPath = path.join(tmpRoot, 'entry.ts'); 13 + 14 + fs.mkdirSync(path.join(packageRoot, 'util'), { recursive: true }); 15 + 16 + fs.writeFileSync( 17 + path.join(packageRoot, 'package.json'), 18 + JSON.stringify( 19 + { 20 + name: '@acme/ts-type-imports', 21 + type: 'module', 22 + exports: { 23 + import: './util/loader_mjs.mjs' 24 + } 25 + }, 26 + null, 27 + 2 28 + ) 29 + ); 30 + 31 + fs.writeFileSync(path.join(packageRoot, 'util', 'loader_mjs.mjs'), ['export const RuntimeThing = 42;', ''].join('\n')); 32 + 33 + fs.writeFileSync( 34 + scriptPath, 35 + [ 36 + "import { RuntimeThing, MissingType, MissingTypeTwo } from '@acme/ts-type-imports';", 37 + 'const one = null as unknown as MissingType;', 38 + 'const two = null as unknown as MissingTypeTwo;', 39 + 'void one;', 40 + 'void two;', 41 + 'console.log(RuntimeThing);', 42 + '' 43 + ].join('\n') 44 + ); 45 + 46 + const result = spawnSync(process.execPath, [scriptPath], { 47 + cwd: tmpRoot, 48 + encoding: 'utf8' 49 + }); 50 + 51 + if (result.error) throw result.error; 52 + 53 + assert( 54 + result.status === 0, 55 + `expected type-only imports in .ts entrypoint to be erased, got ${result.status}\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}` 56 + ); 57 + assert(result.stdout === '42\n', `expected stdout to be 42, got ${JSON.stringify(result.stdout)}`); 58 + assert( 59 + !/does not provide an export named 'MissingType'/.test(result.stderr), 60 + `expected no missing named export error for type-only import, got stderr:\n${result.stderr}` 61 + ); 62 + 63 + console.log('TypeScript type-only import elision works');