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

Typescript 声明类型时似乎未能得到完整支持? #40

Closed
fjqingyou opened this issue Oct 12, 2022 · 10 comments
Closed

Typescript 声明类型时似乎未能得到完整支持? #40

fjqingyou opened this issue Oct 12, 2022 · 10 comments

Comments

@fjqingyou
Copy link

import ProvinceCityChina from "province-city-china";

export class Test{
    protected provinceObj : ProvinceCityChina.Province | null = null;//命名空间“"province-city-china"”没有已导出的成员“Province”。ts(2694)
}

请问是否我的用法有误?

@fjqingyou
Copy link
Author

如果是在 nodejs 环境下倒是可以用

export class Test{
    protected provinceObj : globalThis.ProvinceCityChina.Province | null = null;
}

这种方式声明,不过这样它就附带了个 globalThis 的前缀,会降低代码的移植性。请问是否有更合适的使用方式?

@jaywcjlove
Copy link
Member

@fjqingyou 我测试下来没有问题呢。

image

没有默认到出方法,需要下面方式导入包?

import data from 'province-city-china'
import dataCity from  'province-city-china/dist/city.json'

image

jaywcjlove added a commit that referenced this issue Oct 12, 2022
@fjqingyou
Copy link
Author

fjqingyou commented Oct 12, 2022

通过调用推导类型是正常的可以得到目标类型的。

不过如果不是赋值形式,而是声明某个变量是什么类型的变量时。

例如:
let province : ProvinceCityChina.Province;//只声明,还不赋值

只声明一个变量以及它的类型,但是具体的值还未确定,需要后续逻辑进一步计算才能得到真正的值。

那么它就不属于推导性质可知的类型,而 ProvinceCityChina.Province 这个类型是未导出的!

jaywcjlove added a commit that referenced this issue Oct 12, 2022
jaywcjlove added a commit that referenced this issue Oct 12, 2022
github-actions bot pushed a commit that referenced this issue Oct 12, 2022
@jaywcjlove
Copy link
Member

@fjqingyou Upgrade v8.5.2

import { City } from 'province-city-china'

import type ProvinceCityChina from '@province-city-china/types'

const data: Partial<City> = { code: '12', name: 'dsfsdf'}
const dt: Partial<ProvinceCityChina.Area> = {}

@fjqingyou
Copy link
Author

我在 vscode 中新建了一个 Test.ts ,复制了这个例子后,似乎并未按预期工作。是否是我的用法错误?

image

@jaywcjlove
Copy link
Member

@fjqingyou Upgrade v8.5.4, 找到原因了,修复了。

github-actions bot pushed a commit that referenced this issue Oct 12, 2022
@fjqingyou
Copy link
Author

已经按预期工作,感谢!

@fjqingyou
Copy link
Author

@jaywcjlove 似乎 Country 类型的并未被导出?

@fjqingyou fjqingyou reopened this Oct 13, 2022
jaywcjlove added a commit that referenced this issue Oct 13, 2022
jaywcjlove added a commit that referenced this issue Oct 13, 2022
@jaywcjlove
Copy link
Member

@fjqingyou Upgrade v8.5.5

github-actions bot pushed a commit that referenced this issue Oct 13, 2022
@fjqingyou
Copy link
Author

@jaywcjlove 它正常工作了!感谢!

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

2 participants