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

feat: support custom definitions #117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

feat: support custom definitions #117

wants to merge 2 commits into from

Conversation

aliuq
Copy link

@aliuq aliuq commented Nov 26, 2023

Description 描述

允许用户基于已存在的 schema 添加额外的类型声明

Linked Issues 关联的 Issues

#116

Additional context 额外上下文

  1. 对于 lang="yaml",不知道为啥也能给出提示?
  2. 对 volar 和智能提示这块不太懂,不确认是不是会有性能问题

参考项目:volar-plugin-vue-router

@KeJunMao KeJunMao requested a review from nei1ee November 27, 2023 01:56
@KeJunMao
Copy link
Member

总觉得实现怪怪得呢

@aliuq
Copy link
Author

aliuq commented Nov 27, 2023

总觉得实现怪怪得呢

它的意思是为内部的 packages\schema\schema.json 扩展类型,但是好像除了 PageMetaDatum,其余的都不是动态的?还有个问题,我不知道怎么打印 volar 执行的日志,应该从哪里能看到日志呀?

import { PageMetaDatum as SPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages';

export interface UniPagesRouteMeta {
  PageMetaDatum: Partial<SPageMetaDatum> & {
    /** 自定义属性 */
    customAttribute?: string
  }
  H5: {
    /** 自定义属性 */
    customAttribute?: string
  }
  PagesConfig: {
    /** 自定义属性 */
    customAttribute?: string
  }
}

@nei1ee
Copy link
Member

nei1ee commented Nov 27, 2023

总觉得实现怪怪得呢

我也觉得,但也没有其他好的方法吧

@KeJunMao
Copy link
Member

日志可以从这里看!

image

@KeJunMao
Copy link
Member

  1. 对于 lang="yaml",不知道为啥也能给出提示?

意思是不修改 yamlLs 那个文件也行?

@aliuq
Copy link
Author

aliuq commented Nov 27, 2023

  1. 对于 lang="yaml",不知道为啥也能给出提示?

意思是不修改 yamlLs 那个文件也行?

对的,我按快捷提示,也能出来

@KeJunMao
Copy link
Member

KeJunMao commented Nov 27, 2023

yaml: https://github.com/volarjs/services/blob/master/packages/yaml/
json: https://github.com/volarjs/services/blob/master/packages/json

俺是参考 volar 的这两个包写的,开始只支持了 json,后来发现可以直接搞多个 ls,然后就一起支持了

@KeJunMao KeJunMao linked an issue Nov 27, 2023 that may be closed by this pull request
@KeJunMao KeJunMao added the enhancement New feature or request label Nov 27, 2023
@aliuq
Copy link
Author

aliuq commented Nov 27, 2023

不理解,也不是执行顺序问题。
我为 H5 添加了自定义属性,但是并不会生效,是因为它们不会被 volar 使用吗?

@aliuq
Copy link
Author

aliuq commented Nov 27, 2023

感觉不太好,这样在 packages\playground\pages.config.ts 中的 pages 不会有提示

@KeJunMao
Copy link
Member

晚点我来看看

@aliuq
Copy link
Author

aliuq commented Nov 27, 2023

@KeJunMao 换成了只修改 PageMetaDatum,方便的时候瞅瞅看

@KeJunMao
Copy link
Member

// custom.d.ts
export {}

declare module '@uni-helper/vite-plugin-uni-pages' {
  interface PageMetaDatum {
    /** 自定义属性 */
    customAttribute?: string
  }
}

假设有以上文件,是否可以直接从 workspace 拿到 tsconfig ,然后通过 ts api 拿到 PageMetaDatum 接口的所有 type definitions,最后判断是不是有被扩展过。如果有就动态生成,然后合并,如果没有就直接用默认的。

这样的好处是可以 0 配置

@aliuq
Copy link
Author

aliuq commented Nov 28, 2023

@KeJunMao 相同的 PageMetaDatum, volar 报了个类型重复的错误 typescript Type "PageMetaDatum" has multiple definitions

import { PageMetaDatum as IPageMetaDatum } from '@uni-helper/vite-plugin-uni-pages';

interface PageMeta {
  /** 自定义属性 */
  customAttribute?: string
}
// volar 需要拿到内置的和自定义的
interface ExtraPageMetaDatum extends PageMeta, Partial<IPageMetaDatum> { }

// ts 需要自定义的
declare module '@uni-helper/vite-plugin-uni-pages' {
  interface PageMetaDatum extends PageMeta { }
}

@aliuq
Copy link
Author

aliuq commented Nov 28, 2023

// custom.d.ts
export {}

declare module '@uni-helper/vite-plugin-uni-pages' {
  interface PageMetaDatum {
    /** 自定义属性 */
    customAttribute?: string
  }
}

假设有以上文件,是否可以直接从 workspace 拿到 tsconfig ,然后通过 ts api 拿到 PageMetaDatum 接口的所有 type definitions,最后判断是不是有被扩展过。如果有就动态生成,然后合并,如果没有就直接用默认的。

这样的好处是可以 0 配置

这个有点深了,我不太会,最近时间也少了

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

Successfully merging this pull request may close these issues.

如何为 route 块里添加的属性补充类型声明
3 participants