StencilJS - TypeScript - "Cannot find name ..." 导出枚举时
StencilJS - TypeScript - "Cannot find name ..." when exporting an enum
我有以下项目:
https://github.com/napolev/stencil-cannot-find-name
其中包含以下两个文件:
自定义-container.tsx
import { Component, Element, State } from '@stencil/core';
@Component({
tag: 'custom-container',
styleUrl: 'custom-container.scss',
})
export class WebComponent {
@Element() el!: HTMLStencilElement;
@State() label: String = '<empty>';
componentDidLoad() {
document.querySelector('.button_get_anchor').addEventListener('click', () => {
let position = '<unset>';
position = this.el.querySelector('custom-details').getDefaultKnobEPosition();
this.label = position;
});
}
render() {
return [
<div class="label">{this.label}</div>,
<custom-details></custom-details>,
<div>
<button class="button_get_anchor">Get Anchor</button>
</div>
];
}
}
自定义-details.tsx
import { Component, Method } from '@stencil/core';
@Component({
tag: 'custom-details',
styleUrl: 'custom-details.scss',
})
export class WebComponent {
render() {
return [
<div class="details">This is the "custom-details"</div>
];
}
@Method()
sayHelloWorldOnConsole() {
console.log('Hello World!');
}
//*
@Method()
getDefaultKnobEPosition(): Anchor {
return Anchor.Left;
}
//*/
}
export enum Anchor {
Left = 'left',
Center = 'center',
Right = 'right',
}
我的问题是:当我运行:
$ npm start --es5
我收到以下错误:
[ ERROR ] TypeScript: ./stencil-cannot-find-name/src/components.d.ts:66:39
Cannot find name 'Anchor'.
L65: interface CustomDetails {
L66: 'getDefaultKnobEPosition': () => Anchor;
L67: 'sayHelloWorldOnConsole': () => void;
[21:56.0] dev server: http://localhost:3333/
[21:56.0] build failed, watching for changes... in
15.59 s
如下图所示:
此外,甚至在使用 npm
进行编译之前,在 Visual Studio Code
上我会收到有关该问题的通知,如下图所示:
这是导致问题的行:
https://github.com/napolev/stencil-cannot-find-name/blob/master/src/components.d.ts#L66
上面那个文件是 auto-generated
,所以我无法修改它来解决问题。
知道如何解决这个问题吗?
谢谢!
如果文件是自动生成的,那你就完蛋了。 components.d.ts
文件可以进行类型检查的唯一方法是它是否可以访问 Anchor
通过将其导入
import {Anchor} from './custom-details';
您可以通过将 Anchor 注入全局范围来使其成为 TypeCheck,从而使其对编译上下文中的所有文件可见
自定义-details.tsx
export class WebComponent {
// ....
}
declare global {
enum Anchor {
Left = 'left',
Center = 'center',
Right = 'right'
}
}
(<{Anchor: typeof Anchor}>window.Stencil).Anchor = {
Left = 'left',
Center = 'center',
Right = 'right'
};
但你真的、真的不想这样做!
这会污染一个类型的全局命名空间,更糟糕的是,一个值!
提交错误报告。
将 Anchor
放入其自己的文件中可解决构建问题:
/src/components/custom-details/Anchor.ts
export enum Anchor {
Left = 'left',
Center = 'center',
Right = 'right',
}
/src/components/custom-details/custom-details.tsx
import { Component, Method } from '@stencil/core';
import { Anchor } from './Anchor';
@Component({
tag: 'custom-details',
styleUrl: 'custom-details.scss',
})
export class WebComponent {
render() {
return [
<div class="details">This is the "custom-details"</div>
];
}
@Method()
sayHelloWorldOnConsole() {
console.log('Hello World!');
}
//*
@Method()
getDefaultKnobEPosition(): Anchor {
return Anchor.Left;
}
//*/
}
我有以下项目:
https://github.com/napolev/stencil-cannot-find-name
其中包含以下两个文件:
自定义-container.tsx
import { Component, Element, State } from '@stencil/core';
@Component({
tag: 'custom-container',
styleUrl: 'custom-container.scss',
})
export class WebComponent {
@Element() el!: HTMLStencilElement;
@State() label: String = '<empty>';
componentDidLoad() {
document.querySelector('.button_get_anchor').addEventListener('click', () => {
let position = '<unset>';
position = this.el.querySelector('custom-details').getDefaultKnobEPosition();
this.label = position;
});
}
render() {
return [
<div class="label">{this.label}</div>,
<custom-details></custom-details>,
<div>
<button class="button_get_anchor">Get Anchor</button>
</div>
];
}
}
自定义-details.tsx
import { Component, Method } from '@stencil/core';
@Component({
tag: 'custom-details',
styleUrl: 'custom-details.scss',
})
export class WebComponent {
render() {
return [
<div class="details">This is the "custom-details"</div>
];
}
@Method()
sayHelloWorldOnConsole() {
console.log('Hello World!');
}
//*
@Method()
getDefaultKnobEPosition(): Anchor {
return Anchor.Left;
}
//*/
}
export enum Anchor {
Left = 'left',
Center = 'center',
Right = 'right',
}
我的问题是:当我运行:
$ npm start --es5
我收到以下错误:
[ ERROR ] TypeScript: ./stencil-cannot-find-name/src/components.d.ts:66:39
Cannot find name 'Anchor'.
L65: interface CustomDetails {
L66: 'getDefaultKnobEPosition': () => Anchor;
L67: 'sayHelloWorldOnConsole': () => void;
[21:56.0] dev server: http://localhost:3333/
[21:56.0] build failed, watching for changes... in
15.59 s
如下图所示:
此外,甚至在使用 npm
进行编译之前,在 Visual Studio Code
上我会收到有关该问题的通知,如下图所示:
这是导致问题的行:
https://github.com/napolev/stencil-cannot-find-name/blob/master/src/components.d.ts#L66
上面那个文件是 auto-generated
,所以我无法修改它来解决问题。
知道如何解决这个问题吗?
谢谢!
如果文件是自动生成的,那你就完蛋了。 components.d.ts
文件可以进行类型检查的唯一方法是它是否可以访问 Anchor
通过将其导入
import {Anchor} from './custom-details';
您可以通过将 Anchor 注入全局范围来使其成为 TypeCheck,从而使其对编译上下文中的所有文件可见
自定义-details.tsx
export class WebComponent {
// ....
}
declare global {
enum Anchor {
Left = 'left',
Center = 'center',
Right = 'right'
}
}
(<{Anchor: typeof Anchor}>window.Stencil).Anchor = {
Left = 'left',
Center = 'center',
Right = 'right'
};
但你真的、真的不想这样做!
这会污染一个类型的全局命名空间,更糟糕的是,一个值!
提交错误报告。
将 Anchor
放入其自己的文件中可解决构建问题:
/src/components/custom-details/Anchor.ts
export enum Anchor {
Left = 'left',
Center = 'center',
Right = 'right',
}
/src/components/custom-details/custom-details.tsx
import { Component, Method } from '@stencil/core';
import { Anchor } from './Anchor';
@Component({
tag: 'custom-details',
styleUrl: 'custom-details.scss',
})
export class WebComponent {
render() {
return [
<div class="details">This is the "custom-details"</div>
];
}
@Method()
sayHelloWorldOnConsole() {
console.log('Hello World!');
}
//*
@Method()
getDefaultKnobEPosition(): Anchor {
return Anchor.Left;
}
//*/
}