Skip to content

Commit

Permalink
Fix type definitions for moduleResolution nodenext (#70)
Browse files Browse the repository at this point in the history
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 developit/microbundle#1019 (comment)
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.
  • Loading branch information
trygveaa committed Feb 22, 2023
1 parent 5b97bf8 commit 8a6eab6
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 26 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -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<T> = { [P in keyof T]: T[P] | null };
Expand Down
6 changes: 3 additions & 3 deletions src/event-dispatcher.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/graph-edge.ts
Original file line number Diff line number Diff line change
@@ -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}.
Expand Down
11 changes: 5 additions & 6 deletions src/graph-node.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 3 additions & 4 deletions src/graph.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
4 changes: 2 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"paths": {
"property-graph": ["./"]
},
"moduleResolution": "node",
"moduleResolution": "nodenext",
"lib": ["es2020", "dom"],
"target": "es2020",
"module": "es2020",
Expand Down

0 comments on commit 8a6eab6

Please sign in to comment.