英文:
`require` return is not typed properly (is `any`)
问题
我正在尝试将Tailwind自定义插件逻辑移动到单独的TypeScript文件中,但遇到了类型错误:
require
函数对 plugin
函数返回了 any
类型。
我尝试过使用我在Tailwind中声明的 PluginCreator
类型进行断言,但是出现了错误:无法找到名称 'PluginCreator'。
将不胜感激地接受任何示例。
英文:
I'm trying to move tailwind custom plugin logic to a separate TypeScript file, but getting typing error:
>require
function is returning type any
for plugin
function.
I've tried assertion with PluginCreator
type, which I declared in tailwind, but getting error: Cannot find name 'PluginCreator'.
Will appreciate any example.
答案1
得分: 1
以下是翻译好的部分:
使用require
的Typescript中的正确语法是
import const_name = require('pachage_name')
这将编译为
// js
const const_name = require('pachage_name')
在你的情况下,使用
import plugin = require('tailwindcss/plugin')
如果你可以使用现代的导入语法,最好已经使用它 - require
正在逐渐过时。
// 现代的ES模块导入语法:
import plugin from 'tailwindcss/plugin'
这大致编译成相同的js
// 实际的js会有一些兼容包装
// 如果tsconfig中`esModuleInterop`为`true`,这将起到相同的作用
const plugin = require('tailwindcss/plugin')
英文:
The proper syntax for using require
in Typescript is
import const_name = require('pachage_name')
which will compile to
// js
const const_name = require('pachage_name')
In your case, use
import plugin = require('tailwindcss/plugin')
If you can use the modern import syntax, it's better to use it already - require
is slowly becoming outdated.
// the modern ESModule import syntax:
import plugin from 'tailwindcss/plugin'
which is compiled roughly to into the same js
// actuall js will have some compability wrappers
// if tsconfig `esModuleInterop` is `true` this will work the same
const plugin = require('tailwindcss/plugin')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论