I have two Rush packages inside of a monorepo. Lets call them Package-A
and Package-B
.
There are some TS type definitions that live in Package-A
that I want to be able to access within Package-B
.
// Package-A/src/index.ts
export * from './types';
// Package-A/src/types.ts
import type { Props as PackageAComponentPropsForExport } from '~/components/PackageAComponent';
export type PackageAComponentProps = PackageAComponentPropsForExport;
// Package-A/src/components/PackageAComponent
export type Props = {
name: string;
age: number;
};
tsconfig.json
for Package-A
:
{
"include": [
"src/**/*.ts",
"src/**/*.d.ts",
"src/**/*.tsx",
"src/**/*.js",
"src/**/*.jsx",
"./types/**/*.ts",
"./types/**/*.d.ts",
"./*.js",
"./.*.js",
"./*.json",
"./.*.json"
],
"exclude": [
"node_modules",
"cjs",
"lib"
],
"compilerOptions": {
"jsx": "react",
"typeRoots": [
"./node_modules/@types",
"./types"
],
"allowSyntheticDefaultImports": true,
"preserveSymlinks": true,
"baseUrl": ".",
"esModuleInterop": true,
"lib": [
"dom",
"es2020"
],
"moduleResolution": "node",
"noEmit": true,
"noErrorTruncation": true,
"noImplicitAny": true,
"paths": {
"~/*": [
"src/*"
]
},
"plugins": [{
"transform": "@zerollup/ts-transform-paths"
}],
"resolveJsonModule": true,
"skipLibCheck": true,
"strict": true,
"types": [
"node",
"jest",
]
}
}
// Package-B/src/app/index.tsx
import type { PackageAComponentProps } from 'Package-A';
// 👇 I expect this to error, but instead it says it is aliased to 'any'.
const test: PackageAComponentProps = {};
When I import the types into Package-B
, the types are found / can be imported, however they do not contain any type information. The seem to be aliased to any
, and I do not get type hinting in my editor.
I have tried to export the types in a variety of different ways, including:
// Package-A/src/types.ts
export type { Props as PackageAComponentPropsForExport } from '~/components/PackageAComponent';
// Package-A/src/types.ts
import type { Props as PackageAComponentPropsForExport } from '~/components/PackageAComponent';
export type PackageAComponentProps = PackageAComponentPropsForExport;
import()
syntax:// Package-A/src/types.ts
export type PackageAComponentProps = import('~/components/PackageAComponent').Props;
...however, I get the same result with all three. It seems to just export the type but alias its typing information to any
.
In your tsconfig.json
, you need to set "declaration": true
:
{
"compilerOptions": {
"declaration": true, // generate typings
"declarationMap": true, // generate source maps, if desired
"declarationDir": "types", // explicit directory
// ...
},
// ...
}
The tsc
will automatically resolve and export the necessary .d.ts
files. You shouldn't need to export anything from your module except for the API by which people will utilization your module.