From bc782cee98096d2562d87c7a0b15051b6d0d6628 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Wed, 13 Mar 2024 22:37:56 -0400 Subject: fix(node): resolve types via package.json for directory import (#22878) Does a package resolve when resolving types for a directory (copying the behaviour that typescript does). --- .../specs/npm/check_pkg_json_import/__test__.json | 5 ++ tests/specs/npm/check_pkg_json_import/main.out | 3 + tests/specs/npm/check_pkg_json_import/main.ts | 18 +++++ .../1.0.0/hooks/src/index.d.ts | 4 ++ .../types-pkg-json-import/1.0.0/package.json | 14 ++++ .../types-pkg-json-import/1.0.0/src/index.d.ts | 76 ++++++++++++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 tests/specs/npm/check_pkg_json_import/__test__.json create mode 100644 tests/specs/npm/check_pkg_json_import/main.out create mode 100644 tests/specs/npm/check_pkg_json_import/main.ts create mode 100644 tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/hooks/src/index.d.ts create mode 100644 tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/package.json create mode 100644 tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/src/index.d.ts (limited to 'tests') diff --git a/tests/specs/npm/check_pkg_json_import/__test__.json b/tests/specs/npm/check_pkg_json_import/__test__.json new file mode 100644 index 000000000..ce8e53280 --- /dev/null +++ b/tests/specs/npm/check_pkg_json_import/__test__.json @@ -0,0 +1,5 @@ +{ + "base": "npm", + "args": "check --all main.ts", + "output": "main.out" +} diff --git a/tests/specs/npm/check_pkg_json_import/main.out b/tests/specs/npm/check_pkg_json_import/main.out new file mode 100644 index 000000000..1f436e613 --- /dev/null +++ b/tests/specs/npm/check_pkg_json_import/main.out @@ -0,0 +1,3 @@ +Download http://localhost:4545/npm/registry/@denotest/types-pkg-json-import +Download http://localhost:4545/npm/registry/@denotest/types-pkg-json-import/1.0.0.tgz +Check file:///[WILDLINE]/main.ts diff --git a/tests/specs/npm/check_pkg_json_import/main.ts b/tests/specs/npm/check_pkg_json_import/main.ts new file mode 100644 index 000000000..72b887735 --- /dev/null +++ b/tests/specs/npm/check_pkg_json_import/main.ts @@ -0,0 +1,18 @@ +import { createContext } from "npm:@denotest/types-pkg-json-import"; +import { useContext } from "npm:@denotest/types-pkg-json-import/hooks"; + +export interface Foo { + foo: string; +} + +export const CTX = createContext(undefined); + +function unwrap(foo: Foo) {} + +export function useCSP() { + const foo = useContext(CTX); + // previously this was erroring + if (foo) { + unwrap(foo); + } +} diff --git a/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/hooks/src/index.d.ts b/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/hooks/src/index.d.ts new file mode 100644 index 000000000..a0fe33a3d --- /dev/null +++ b/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/hooks/src/index.d.ts @@ -0,0 +1,4 @@ +// this directory import was not working (it should resolve via the package.json) +import { PreactContext } from '../..'; + +export declare function useContext(context: PreactContext): T; diff --git a/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/package.json b/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/package.json new file mode 100644 index 000000000..3f9792f22 --- /dev/null +++ b/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/package.json @@ -0,0 +1,14 @@ +{ + "name": "@denotest/types-directory-import", + "version": "1.0.0", + "exports": { + ".": { + "types": "./src/index.d.ts", + "import": "./dist/preact.mjs" + }, + "./hooks": { + "types": "./hooks/src/index.d.ts", + "import": "./hooks/dist/hooks.mjs" + } + } +} \ No newline at end of file diff --git a/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/src/index.d.ts b/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/src/index.d.ts new file mode 100644 index 000000000..94e6b2572 --- /dev/null +++ b/tests/testdata/npm/registry/@denotest/types-pkg-json-import/1.0.0/src/index.d.ts @@ -0,0 +1,76 @@ +export as namespace preact; + +export interface VNode

{ + type: any | string; + props: P & { children: ComponentChildren }; + key: Key; + /** + * ref is not guaranteed by React.ReactElement, for compatibility reasons + * with popular react libs we define it as optional too + */ + ref?: Ref | null; + /** + * The time this `vnode` started rendering. Will only be set when + * the devtools are attached. + * Default value: `0` + */ + startTime?: number; + /** + * The time that the rendering of this `vnode` was completed. Will only be + * set when the devtools are attached. + * Default value: `-1` + */ + endTime?: number; +} + +export type Key = string | number | any; + +export type RefObject = { current: T | null }; +export type RefCallback = (instance: T | null) => void; +export type Ref = RefObject | RefCallback | null; + +export type ComponentChild = + | VNode + | object + | string + | number + | bigint + | boolean + | null + | undefined; +export type ComponentChildren = ComponentChild[] | ComponentChild; + +export interface FunctionComponent

{ + (props: any, context?: any): VNode | null; + displayName?: string; + defaultProps?: Partial

| undefined; +} +export interface FunctionalComponent

extends FunctionComponent

{} + +// +// Context +// ----------------------------------- +export interface Consumer + extends FunctionComponent<{ + children: (value: T) => ComponentChildren; + }> {} +export interface PreactConsumer extends Consumer {} + +export interface Provider + extends FunctionComponent<{ + value: T; + children?: ComponentChildren; + }> {} +export interface PreactProvider extends Provider {} +export type ContextType> = C extends Context + ? T + : never; + +export interface Context { + Consumer: Consumer; + Provider: Provider; + displayName?: string; +} +export interface PreactContext extends Context {} + +export function createContext(defaultValue: T): Context; -- cgit v1.2.3