Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node): resolve types via package.json for directory import #22878

Merged
merged 6 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Start working on directory resolution issue
  • Loading branch information
dsherret committed Mar 12, 2024
commit 7f79018e56a5a4d5326ea51acc83c01a97461b09
13 changes: 13 additions & 0 deletions tests/specs/npm/check_pkg_json_import/main.out
dsherret marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 1,13 @@
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
error: TS2345 [ERROR]: Argument of type '{}' is not assignable to parameter of type 'Foo'.
Property 'foo' is missing in type '{}' but required in type 'Foo'.
unwrap(foo);
~~~
at file:///[WILDLINE]/main.ts:15:12

'foo' is declared here.
foo: string;
~~~
at file:///[WILDLINE]/main.ts:5:3
17 changes: 17 additions & 0 deletions tests/specs/npm/check_pkg_json_import/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,17 @@
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<Foo | undefined>(undefined);

function unwrap(foo: Foo) {}

export function useCSP() {
const foo = useContext(CTX);
if (foo) {
unwrap(foo);
}
}
5 changes: 5 additions & 0 deletions tests/specs/npm/check_pkg_json_import/test.json
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
{
"base": "npm",
"args": "check --all main.ts",
"output": "main.out"
}
Original file line number Diff line number Diff line change
@@ -0,0 1,4 @@
// this directory import was not working (it should resolve via the package.json)
import { PreactContext } from '../..';

export declare function useContext<T>(context: PreactContext<T>): T;
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 1,76 @@
export as namespace preact;

export interface VNode<P = {}> {
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<any> | 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<T> = { current: T | null };
export type RefCallback<T> = (instance: T | null) => void;
export type Ref<T> = RefObject<T> | RefCallback<T> | null;

export type ComponentChild =
| VNode<any>
| object
| string
| number
| bigint
| boolean
| null
| undefined;
export type ComponentChildren = ComponentChild[] | ComponentChild;

export interface FunctionComponent<P = {}> {
(props: any, context?: any): VNode<any> | null;
displayName?: string;
defaultProps?: Partial<P> | undefined;
}
export interface FunctionalComponent<P = {}> extends FunctionComponent<P> {}

//
// Context
// -----------------------------------
export interface Consumer<T>
extends FunctionComponent<{
children: (value: T) => ComponentChildren;
}> {}
export interface PreactConsumer<T> extends Consumer<T> {}

export interface Provider<T>
extends FunctionComponent<{
value: T;
children?: ComponentChildren;
}> {}
export interface PreactProvider<T> extends Provider<T> {}
export type ContextType<C extends Context<any>> = C extends Context<infer T>
? T
: never;

export interface Context<T> {
Consumer: Consumer<T>;
Provider: Provider<T>;
displayName?: string;
}
export interface PreactContext<T> extends Context<T> {}

export function createContext<T>(defaultValue: T): Context<T>;