如何正确地注释可以接受Component或string作为html标签的组件道具?(How to correctly annotate component props that can accept Component or string as html tag?)
如何正确注释接受
Component
或string作为html标记的组件道具?例如,我有一个可以采用自定义组件的组件(包括像
div
,p
等html标签)。 它是这样写的:interface MyComponentProps<T> { comp: React.ComponentType<T> } function MyComponent<T>({ comp: Component }: MyComponentProps<T>) { return <Component /> }
如果我将另一个组件作为
comp
道具传递给MyComponent
那么Typescript不会抱怨:function Wrapper() { return <div /> } function render() { return <MyComponent comp={Wrapper} /> }
但是,当我通过这样的标准html标签时:
function render() { return <MyComponent comp="div"/> }
打字稿编译器投诉:
Type '{ comp: "div"; }' is not assignable to type 'IntrinsicAttributes & MyComponentProps<{}>'. Type '{ comp: "div"; }' is not assignable to type 'MyComponentProps<{}>'. Types of property 'comp' are incompatible. Type '"div"' is not assignable to type 'ComponentType<{}>'.
那么,如何在不使用
any
类型的情况下注释像MyComponent
这样的组件?How to correctly annotate component props that accept
Component
or string as html tag?For example, I have a component that can take custom Component (includes html tag like
div
,p
etc.). It's written like this:interface MyComponentProps<T> { comp: React.ComponentType<T> } function MyComponent<T>({ comp: Component }: MyComponentProps<T>) { return <Component /> }
Typescript doesn't complaint if I pass another component as
comp
props toMyComponent
like this:function Wrapper() { return <div /> } function render() { return <MyComponent comp={Wrapper} /> }
But, when I pass standard html tag like this:
function render() { return <MyComponent comp="div"/> }
typescript compiler complaints:
Type '{ comp: "div"; }' is not assignable to type 'IntrinsicAttributes & MyComponentProps<{}>'. Type '{ comp: "div"; }' is not assignable to type 'MyComponentProps<{}>'. Types of property 'comp' are incompatible. Type '"div"' is not assignable to type 'ComponentType<{}>'.
So, how can I annotate a component like
MyComponent
without usingany
type?
原文:https://stackoverflow.com/questions/46072333
最满意答案
似乎你可能忽略了错误。 对我来说,这条线
y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE)
给出错误
dgamma中的错误(length(x),shape = 3,rate = 1/29,scale = 1 / rate,log = FALSE):未找到对象'rate'
所以你的
y
变量永远不会被设置。 您必须从其他一些与x
不同长度的代码中剩下一个。 通过查看length(x)
和长度(y),您应该在绘图之前进行检查。 使用y <- dgamma(x, shape = 3, rate = 1/29, log = FALSE)
代替。 这将使用您想要的
rate
的默认值。 也许你正在查看函数的默认参数值? 当您调用某个函数时,您无法在为参数传递值时指定其他参数的名称,您只能在定义函数时执行此操作。你的
plot()
命令也会返回一个错误。 参数名称是xlab=
和ylab=
而不是xlabel=
和ylabel=
Seems like you are probably ignoring errors. For me, the line
y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE)
gives the error
Error in dgamma(length(x), shape = 3, rate = 1/29, scale = 1/rate, log = FALSE) : object 'rate' not found
so your
y
variable is never set. You must have a left over one from some other code that is a different length thanx
. You should check before plotting by looking atlength(x)
and `length(y). Usey <- dgamma(x, shape = 3, rate = 1/29, log = FALSE)
instead. This will use the default value for
rate
which is what you want. Maybe you were looking at the default parameter values for the function? You can't specify names of other parameters when passing values for a parameter when you call a function, you can only do this when you define a function.Also your
plot()
command returns an error. The parameter names arexlab=
andylab=
and notxlabel=
andylabel=
相关问答
更多-
澄清R中的绘图命令(Clarifying the plot command in R)[2022-11-10]
通常,R知道回收,即在许多情况下,如果它们没有所需的长度,则R 回收载体,例如 1:4 + 1:2 向量1:2被循环使用,因此有效地变为c(1,2,1,2)以适合另一个向量的长度。 现在,我们可以对矢量求和 c(1,2,3,4) + c(1,2,1,2) 在col参数的示例中也是如此。 矢量col被回收,因此它适合x的长度。 但是如您所见, 并非所有参数值都会自动循环使用 。 在图中的x和y参数的情况下,情况并非如此。 这是有道理的,因为如果这两个矢量的长度不匹配,它可能是一个错误。 Generall ... -
我使用performanceanalytics版本1.4.3541时没有收到错误。 也许更新你的版本? 我确实收到警告,当调用charts.PerformanceSummary并使用na.rm = TRUE时,“na.rm不是图形参数”。 出现此警告是因为na.rm = T作为额外参数传递给函数(因为函数具有特殊参数... ),然后将其传递给charts.PerformanceSummary的图表函数,其中包括chart.CumReturns , chart.BarVaR和chart.Drawdown ,所 ...
-
这看起来像你实际想要的。 对于您的具体情况,它适用于与此答案相同的方法: import matplotlib.pyplot as plt fig, ax = plt.subplots(nrows=3, ncols=3, sharex=True, sharey=True, figsize=(6, 6)) fig.text(0.5, 0.04, 'common X', ha='center') fig.text(0.04, 0.5, 'common Y', va='center', rotation='ve ...
-
似乎你可能忽略了错误。 对我来说,这条线 y <- dgamma(x, shape = 3, rate = 1/29, scale = 1/rate, log = FALSE) 给出错误 dgamma中的错误(length(x),shape = 3,rate = 1/29,scale = 1 / rate,log = FALSE):未找到对象'rate' 所以你的y变量永远不会被设置。 您必须从其他一些与x不同长度的代码中剩下一个。 通过查看length(x)和长度(y),您应该在绘图之前进行检查。 使用 ...
-
你应该使用 mydata[, parnamequotes] (返回一个向量)或者, mydata[[parnamequotes]] (返回一个向量)或者, mydata[parnamequotes] (返回一个数据框) 代替mydata$parname (请参阅此问题 )并从函数中删除parname 。 plot_var <- function(dirpath,parnamequotes) { mydata <- read.csv(dirpath, skip=6) par(mfrow=c(1,2)) ...
-
从不使用get文档中set : x = 0:0.01:20; y1 = 200*exp(-0.05*x).*sin(x); y2 = 0.8*exp(-0.5*x).*sin(10*x); figure % new figure hAx = plotyy(x,y1,x,y2); title('Multiple Decay Rates') xlabel('Time (\musec)') ylabel(hAx(1),'Slow Decay') % left y-axis ylabel(hAx(2),'Fa ...
-
'x'和'y'长度在ioslides中不同,但不是单独的('x' and 'y' lengths differ error together but not separately in ioslides)[2021-08-19]
我认为这与放置x / Day1的位置有关。 我通常把它放在像这样的renderPlot函数中,这似乎对我有用: ```{r, echo = FALSE, warning=FALSE} inputPanel( sliderInput("multiplier", label = "Distance:", max = 1.35, min = 1, value = 1, ticks=FALSE, animate=animationOptions(interval = 100, playButton = "run" ... -
您需要更加小心地匹配模型中使用的变量名称以及预测期间使用的变量名称。 您得到的错误是因为preidct函数中data.frame中的名称与模型中的术语名称不匹配,因此您实际上并未预测新值。 问题是predict本质上是从中获取数据 model.frame(~cars$dist, data.frame(dist=c.x)) 所以因为你的公式中明确地有cars$dist ,所以没有从你的newdata参数中获取的“免费”符号。 将其与结果进行比较 model.frame(~dist, data.frame(d ...
-
R中的'x'和'y'长度不同('x' and 'y' lengths differ in R)[2023-12-14]
要绘制财富效用函数的二阶导数的水平线,您需要确保Udprime为每个点w都有一个点。 有两种方法可以做到这一点: 捷径: plot(cbind(w, Udprime)) 更“真实的数学”: Udprime = -beta * w^0 To plot the horizontal line for the second derivative of the wealth utility function, you'll need to make sure Udprime has a point for ev ... -
DAAG:::cv.lm显然不支持你可以用lm做的所有事情,例如,它不支持公式中的函数。 你需要采取一个中间步骤。 mf <- as.data.frame(model.matrix(y ~ poly(x), data = Ultrasonic)) mf$y <- Ultrasonic$y mf$`(Intercept)` <- NULL #sanitize names names(mf) <- make.names(names(mf)) #[1] "poly.x." "y" g.poly.san <- ...