Skip to content

The type define can't be derived correctly #389

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

Open
hustcc opened this issue May 7, 2020 · 7 comments
Open

The type define can't be derived correctly #389

hustcc opened this issue May 7, 2020 · 7 comments

Comments

@hustcc
Copy link

hustcc commented May 7, 2020

Code below:

  • wrapper.ts
export * from 'gl-matrix/vec2';

export function otherFunc() {}
  • test.ts
import * as vec2 from './wrapper'

vec2.length(); // here will occur type define error

In addition, the source code structure is different from npm package, this is not a best practice maybe. We have many ways to organize code for code split, But the way now is not very good.

Do you need a pr?

@stefnotch
Copy link
Collaborator

I'm always open to suggestions or PRs regarding improving gl-matrix!

@hustcc
Copy link
Author

hustcc commented May 8, 2020

I'm always open to suggestions or PRs regarding improving gl-matrix!

I will send the pr today, a lot of diff, but compatibility will be guaranteed.

@hustcc
Copy link
Author

hustcc commented May 8, 2020

  • Plan 1

Generate .d.ts file for each sub-package. But now I do not know how to generate sub-package d.ts file~

  • Plan 2

Rewrite with ts. then generate the sub-packages.

I think Plan2 is better, what's your opinion? @stefnotch

@stefnotch
Copy link
Collaborator

So, basically, I added the type definitions for my main use case which is simply importing everything from "gl-matrix". I didn't take the subpackages into account, as that seemed a fair bit more complex.

import { vec3 } from "gl-matrix";

@TrySound Sorry for the ping, but I thought that you might be an expert on this topic. A quite relevant question here is, do we still need all the subpackages?

@TrySound
Copy link
Contributor

TrySound commented May 9, 2020

do we still need all the subpackages?

Yes, unless you choose unique name for each function.
https://github.com/toji/gl-matrix/blob/master/src/mat2.js#L13
https://github.com/toji/gl-matrix/blob/master/src/mat2d.js#L27

In addition, the source code structure is different from npm package, this is not a best practice maybe

https://unpkg.com/browse/[email protected]/esm/
https://unpkg.com/browse/[email protected]/cjs/
Same structure. Sub packages are there for technical reasons to prevent your bundle bloating.

I didn't take the subpackages into account, as that seemed a fair bit more complex.

@stefnotch Doesn't this syntax assume sub packages? Or is this something like old school namespace?

export module mat2 {

TrySound added a commit to TrySound/gl-matrix that referenced this issue May 9, 2020
Ref toji#389

Emitted all types to directory instead of single file.
This way each sub package can specify own "types" field.

Though there are problems
- tsc do not emit types imports in dts files

I solved by manual prepending import from types

```
import { mat2, mat2d, mat3, mat4, quat, quat2, vec2, vec3, vec4, ReadonlyMat2, ReadonlyMat2d, ReadonlyMat3, ReadonlyMat4, ReadonlyQuat, ReadonlyQuat2, ReadonlyVec2, ReadonlyVec3, ReadonlyVec4 } from './types';
/**
 * Quaternion
 * @module quat
 */
/**
 * Creates a new identity quat
 *
 * @returns {quat} a new quaternion
 */
export function create(): quat;
```

- tsc do not resolve reused function from another module so we end with
this
```
export const fromValues: typeof vec4.fromValues;
// 2693: 'vec4' only refers to a type, but is being used as a value here.
```

- even if we write all imports from another module we have a conflict
between module names and types

```
import { ..., vec2, ... } from './types';
export const fromValues: typeof vec4.fromValues;
```
TrySound added a commit to TrySound/gl-matrix that referenced this issue May 9, 2020
Ref toji#389

Emitted all types to directory instead of single file.
This way each sub package can specify own "types" field.

Though there are problems
- tsc do not emit types imports in dts files

I solved by manual prepending import from types

```
import { mat2, mat2d, mat3, mat4, quat, quat2, vec2, vec3, vec4, ReadonlyMat2, ReadonlyMat2d, ReadonlyMat3, ReadonlyMat4, ReadonlyQuat, ReadonlyQuat2, ReadonlyVec2, ReadonlyVec3, ReadonlyVec4 } from './types';
/**
 * Quaternion
 * @module quat
 */
/**
 * Creates a new identity quat
 *
 * @returns {quat} a new quaternion
 */
export function create(): quat;
```

- tsc do not resolve reused function from another module so we end with
this
```
export const fromValues: typeof vec4.fromValues;
// 2693: 'vec4' only refers to a type, but is being used as a value here.
```

- even if we write all imports from another module we have a conflict
between module names and types

```
import { ..., vec2, ... } from './types';
export const fromValues: typeof vec4.fromValues;
```
@TrySound
Copy link
Contributor

TrySound commented May 9, 2020

See here. Not solved yet. Need assistance.
#390

@stefnotch
Copy link
Collaborator

I took a look at it and am not sure how to solve those issues. Maybe @hustcc has some ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants