Search code examples
typescriptmonorepo

Type is lost (becomes `any`) on reexport when importing from another module


When I try to import a type from one package in PNPM monorepo into another, it's lost if it's not imported directly from source file

// server/src/q1.ts
export const q1 = 123
// server/src/test.ts
export { q1 as q2 } from './q1.ts'
// front/src/test.ts
import type { q1 } from 'server/src/q1'
let t1: typeof q1
//  ^? 123 
import type { q1 as q2 } from 'server/test'
let t2: typeof q2
//  ^? any

Is there an obvious thing I'm missing?
(If not, I will make a MRE pnpm workspace for this)


Solution

  • The code - as provided - would not compile. The problematic line is

    import type { q1 as q2 } from 'server/test'
    

    According to the first two code examples it should be

    import { q2 } from 'server/src/test'
    

    The import type is also confusing, as both q1 and q2 are referencing a value (123), not a type.

    After fixing these issues, both t1 and t2 are reported by Typescript having the value (and type) of 123.