From 0d1731ee6c5397972ea28f5f6e0b2f70295dd918 Mon Sep 17 00:00:00 2001 From: Trygve Aaberge Date: Tue, 21 Feb 2023 11:32:28 +0100 Subject: [PATCH] Fix type definitions for moduleResolution nodenext When importing this library in a project using moduleResolution nodenext, the types would give errors because the import paths were missing the file extension which is required for moduleResolution nodenext. Therefore we have to add .js to all imports of local files. See https://github.com/developit/microbundle/issues/1019#issuecomment-1384721680 and the issues that comment links to for some more detailed explanation of this. I also changed moduleResolution to nodenext in the tsconfig because when it's set to node, typescript won't give errors for missing file extensions which means they are easy to forget. With it set to nodenext, you will get an error if an import is missing the file extension. As far as I can see, changing this doesn't change the build output. --- package.json | 1 + src/constants.ts | 4 ++-- src/event-dispatcher.ts | 6 +++--- src/graph-edge.ts | 4 ++-- src/graph-node.ts | 11 +++++------ src/graph.ts | 7 +++---- src/index.ts | 12 ++++++------ src/utils.ts | 4 ++-- tsconfig.json | 2 +- 9 files changed, 25 insertions(+), 26 deletions(-) diff --git a/package.json b/package.json index 9b0c68b..4af9a3c 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "main": "./dist/property-graph.cjs", "module": "./dist/property-graph.esm.js", "exports": { + "types": "./dist/index.d.ts", "require": "./dist/property-graph.cjs", "default": "./dist/property-graph.modern.js" }, diff --git a/src/constants.ts b/src/constants.ts index a58fcdd..6c80d8e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -1,5 +1,5 @@ -import type { GraphEdge } from './graph-edge'; -import type { GraphNode } from './graph-node'; +import type { GraphEdge } from './graph-edge.js'; +import type { GraphNode } from './graph-node.js'; /** TypeScript utility for nullable types. */ export type Nullable = { [P in keyof T]: T[P] | null }; diff --git a/src/event-dispatcher.ts b/src/event-dispatcher.ts index 4148d1d..e2506ce 100644 --- a/src/event-dispatcher.ts +++ b/src/event-dispatcher.ts @@ -1,6 +1,6 @@ -import type { Graph } from './graph'; -import type { GraphNode } from './graph-node'; -import type { GraphEdge } from './graph-edge'; +import type { Graph } from './graph.js'; +import type { GraphNode } from './graph-node.js'; +import type { GraphEdge } from './graph-edge.js'; export interface BaseEvent { type: string; diff --git a/src/graph-edge.ts b/src/graph-edge.ts index ff7eb74..5fe65be 100644 --- a/src/graph-edge.ts +++ b/src/graph-edge.ts @@ -1,5 +1,5 @@ -import { EventDispatcher, GraphEdgeEvent } from './event-dispatcher'; -import { GraphNode } from './graph-node'; +import { EventDispatcher, GraphEdgeEvent } from './event-dispatcher.js'; +import { GraphNode } from './graph-node.js'; /** * Represents a connection between two {@link GraphNode} resources in a {@link Graph}. diff --git a/src/graph-node.ts b/src/graph-node.ts index 3908d5a..a276ca6 100644 --- a/src/graph-node.ts +++ b/src/graph-node.ts @@ -1,11 +1,10 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/ban-types */ -import { GraphNodeEvent } from '.'; -import { LiteralKeys, Nullable, Ref, RefMap, RefKeys, RefListKeys, RefMapKeys } from './constants'; -import { BaseEvent, EventDispatcher } from './event-dispatcher'; -import { Graph } from './graph'; -import { GraphEdge } from './graph-edge'; -import { isRef, isRefList, isRefMap } from './utils'; +import { LiteralKeys, Nullable, Ref, RefMap, RefKeys, RefListKeys, RefMapKeys } from './constants.js'; +import { BaseEvent, EventDispatcher, GraphNodeEvent } from './event-dispatcher.js'; +import { Graph } from './graph.js'; +import { GraphEdge } from './graph-edge.js'; +import { isRef, isRefList, isRefMap } from './utils.js'; // References: // - https://stackoverflow.com/a/70163679/1314762 diff --git a/src/graph.ts b/src/graph.ts index edc4496..3a66d7b 100644 --- a/src/graph.ts +++ b/src/graph.ts @@ -1,7 +1,6 @@ -import { GraphNodeEvent } from '.'; -import { EventDispatcher, GraphEdgeEvent, GraphEvent } from './event-dispatcher'; -import { GraphEdge } from './graph-edge'; -import { GraphNode } from './graph-node'; +import { EventDispatcher, GraphEdgeEvent, GraphEvent, GraphNodeEvent } from './event-dispatcher.js'; +import { GraphEdge } from './graph-edge.js'; +import { GraphNode } from './graph-node.js'; /** * A graph manages a network of {@link GraphNode} nodes, connected diff --git a/src/index.ts b/src/index.ts index fbfc0ce..f7384a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ -export * from './constants'; // required for type inference -export * from './event-dispatcher'; -export * from './graph'; -export * from './graph-node'; -export * from './graph-edge'; -export * from './utils'; +export * from './constants.js'; // required for type inference +export * from './event-dispatcher.js'; +export * from './graph.js'; +export * from './graph-node.js'; +export * from './graph-edge.js'; +export * from './utils.js'; diff --git a/src/utils.ts b/src/utils.ts index 47d0199..f175ca8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,5 +1,5 @@ -import type { Ref, RefMap } from './constants'; -import { GraphEdge } from './graph-edge'; +import type { Ref, RefMap } from './constants.js'; +import { GraphEdge } from './graph-edge.js'; export function isRef(value: Ref | unknown): boolean { return value instanceof GraphEdge; diff --git a/tsconfig.json b/tsconfig.json index db5b4b5..bdcdf60 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ "paths": { "property-graph": ["./"] }, - "moduleResolution": "node", + "moduleResolution": "nodenext", "lib": ["es2020", "dom"], "target": "es2020", "module": "es2020",