`require`的返回类型未正确标记(为`any`)。

huangapple go评论83阅读模式
英文:

`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.

`require`的返回类型未正确标记(为`any`)。

答案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')

huangapple
  • 本文由 发表于 2023年6月19日 03:33:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定