[英]How can I get fn({param}) of the param type in TypeScript?
I'm using raw emotion package npm i emotion
and bumped to this question.我正在使用原始情感package
npm i emotion
并遇到了这个问题。
If I do this如果我这样做
import { css } from 'emotion';
const test = css({
boxSizing: 'border-box'
})
If I hover the boxSizing
, VSCode tells me that boxSizing
type is如果我 hover
boxSizing
,VSCode 告诉我boxSizing
类型是
"border-box" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "content-box" | BoxSizingProperty[] | undefined
Now, I want to retrieve those type, resulting in this现在,我想检索那些类型,结果是
type result = "border-box" | "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "content-box" | BoxSizingProperty[] | undefined
Is there any utility method I can use to retrieve those types?有什么实用方法可以用来检索这些类型吗?
If I can just do如果我能做到
import { css } from 'emotion';
type result = someUtility<typeof css, 'boxSizing'>
Is it possible to do this without installing any new npm package?是否可以在不安装任何新的 npm package 的情况下执行此操作? I know emotion use
csstype
package for typings under the hood.我知道 emotion 在后台使用
csstype
package 进行打字。
Since you don't want to install another package, here's what you can do:由于您不想安装另一个 package,因此您可以执行以下操作:
import { ObjectInterpolation } from 'emotion'
type BoxSizing = ObjectInterpolation<undefined>['boxSizing'] // type BoxSizing = "-moz-initial" | "inherit" | "initial" | "revert" | "unset" | "border-box" | "content-box" | BoxSizingProperty[] | undefined
You may also find the Parameters
generic type useful, although I don't believe it quite works here since Interpolation<T>
is a union type of several items.您可能还会发现
Parameters
泛型类型很有用,尽管我认为它在这里不太适用,因为Interpolation<T>
是多个项目的联合类型。 Here's an example of how this can be used in another function:这是如何在另一个 function 中使用它的示例:
const example = (params: { boxSizing: 'border-box' }) => undefined
type BoxSizing = Parameters<typeof example>[0]['boxSizing']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.