简体   繁体   English

在打字稿中,我将如何输入这个“撰写”功能

[英]In Typescript how would I type this 'compose' function

I'm in the process of converting my Javascript library that composes styled-components to Typescript (what I've done so far is located on the typescript branch), but I'm having a hard time typing this compose function (well, some of the other composable functions as well, but for now...):我在我的转换Javascript库的同时,工艺组成风格的组件,以打字稿(什么我目前位于进行打字稿分支),但我有一个很难打字compose的功能(当然,有些其他可组合的功能也是如此,但现在......):

Basic usage:基本用法:

const ComposedStyledComponent = compose.t(fn, fn, fn, ...etc)`
  s
`

Breakdown:分解:

compose takes 3 args:
t(ag) = single HTML Element as property of compose or passed as ("string")
f(unctions) = composable functions (withProps(...), withStyles(...), ...etc)  -- see below for full list
s(tyles) = 
   either template literal CSS Properties:
  ` background: ${props => props.primary ? "blue" : "red" }; ` 
   
   or can be an object of CSS Properties: 
   { background: ${props => props.primary ? "blue" : "red" };

What I currently have:我目前拥有的:

import styled, { css } from "styled-components";
import domElements from "../domElements"; // ["a", "abbr", "address", "area", ...etc] 
import { ComponentType } from "../types";

// this needs to be restricted to single usage of ANY of these composable functions: 
// setDisplayName, withAttributes, withDefaultProps, withProps, withPropTypes,  withStyles
type Functions = any[]; 

const extend = (...f: Functions) =>
  f.reduce(
    (a, b) => (...args) => a(b(...args)),
    arg => arg
  );

// can be string: compose.div | compose("div) 
// or  
// it can be a React styled-component: compose(Button)
type Tag = string | ComponentType<any>;

type Interpolation =
  | ((props: Object) => Interpolation)
  | string
  | ComponentType<any>
  | Interpolation[];

type Styles = string[] | Object | ((props: Object) => Interpolation); 

const compose = (t: Tag) => (...f: Functions) => (...s: Styles[]) =>
  extend(...f)(styled(t)` // t is invalid here (see below)
    ${() => css(...s)}; // s is invalid (Expected at least 1 arguments, but got 0 or more.)
  `);

// assigns DOM elements to compose (compose.a, compose.abbr, ...etc)
domElements.forEach((element: string) => {
  // this is complaining about string indexes (see below)
  compose[element] = compose(element);
});

export { compose };

Tag is invalid:标签无效:

 No overload matches this call.
  Overload 1 of 2, '(component: AnyStyledComponent): ThemedStyledFunction<any, any, any, any>', gave the following error.
    Argument of type 'Tag' is not assignable to parameter of type 'AnyStyledComponent'.
      Type 'string' is not assignable to type 'AnyStyledComponent'.
  Overload 2 of 2, '(component: "symbol" | "object" | ComponentClass<any, any> | FunctionComponent<any> | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | ... 165 more ... | "view"): ThemedStyledFunction<...>', gave the following error.

domElement.forEach is invalid: domElement.forEach 无效:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '(t: Tag) => (...f: Functions) => (...s: Styles[]) => any'.
  No index signature with a parameter of type 'string' was found on type '(t: Tag) => (...f: Functions) => (...s: Styles[]) => any'.

For reference, here are the styled-components types .作为参考,这里是styled-components types

Any help would be greatly appreciated.任何帮助将不胜感激。

I'm in the process of converting my Javascript library that composes styled-components to Typescript (what I've done so far is located on the typescript branch), but I'm having a hard time typing this compose function (well, some of the other composable functions as well, but for now...):我在我的转换Javascript库的同时,工艺组成风格的组件,以打字稿(什么我目前位于进行打字稿分支),但我有一个很难打字compose的功能(当然,有些其他可组合的功能也是如此,但现在......):

Basic usage:基本用法:

const ComposedStyledComponent = compose.t(fn, fn, fn, ...etc)`
  s
`

Breakdown:分解:

compose takes 3 args:
t(ag) = single HTML Element as property of compose or passed as ("string")
f(unctions) = composable functions (withProps(...), withStyles(...), ...etc)  -- see below for full list
s(tyles) = 
   either template literal CSS Properties:
  ` background: ${props => props.primary ? "blue" : "red" }; ` 
   
   or can be an object of CSS Properties: 
   { background: ${props => props.primary ? "blue" : "red" };

What I currently have:我目前拥有的:

import styled, { css } from "styled-components";
import domElements from "../domElements"; // ["a", "abbr", "address", "area", ...etc] 
import { ComponentType } from "../types";

// this needs to be restricted to single usage of ANY of these composable functions: 
// setDisplayName, withAttributes, withDefaultProps, withProps, withPropTypes,  withStyles
type Functions = any[]; 

const extend = (...f: Functions) =>
  f.reduce(
    (a, b) => (...args) => a(b(...args)),
    arg => arg
  );

// can be string: compose.div | compose("div) 
// or  
// it can be a React styled-component: compose(Button)
type Tag = string | ComponentType<any>;

type Interpolation =
  | ((props: Object) => Interpolation)
  | string
  | ComponentType<any>
  | Interpolation[];

type Styles = string[] | Object | ((props: Object) => Interpolation); 

const compose = (t: Tag) => (...f: Functions) => (...s: Styles[]) =>
  extend(...f)(styled(t)` // t is invalid here (see below)
    ${() => css(...s)}; // s is invalid (Expected at least 1 arguments, but got 0 or more.)
  `);

// assigns DOM elements to compose (compose.a, compose.abbr, ...etc)
domElements.forEach((element: string) => {
  // this is complaining about string indexes (see below)
  compose[element] = compose(element);
});

export { compose };

Tag is invalid:标签无效:

 No overload matches this call.
  Overload 1 of 2, '(component: AnyStyledComponent): ThemedStyledFunction<any, any, any, any>', gave the following error.
    Argument of type 'Tag' is not assignable to parameter of type 'AnyStyledComponent'.
      Type 'string' is not assignable to type 'AnyStyledComponent'.
  Overload 2 of 2, '(component: "symbol" | "object" | ComponentClass<any, any> | FunctionComponent<any> | "a" | "abbr" | "address" | "area" | "article" | "aside" | "audio" | ... 165 more ... | "view"): ThemedStyledFunction<...>', gave the following error.

domElement.forEach is invalid: domElement.forEach 无效:

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '(t: Tag) => (...f: Functions) => (...s: Styles[]) => any'.
  No index signature with a parameter of type 'string' was found on type '(t: Tag) => (...f: Functions) => (...s: Styles[]) => any'.

For reference, here are the styled-components types .作为参考,这里是styled-components types

Any help would be greatly appreciated.任何帮助将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用打字稿键入此回调函数 - How do I type this callback function using typescript TypeScript:对于这个 function,如何将类型显式传递给 generics - TypeScript: How can I explicitly pass in the type to the generics for this function 如何在 TypeScript 中覆盖标准库 function 的类型? - How do I override type of a standard library function in TypeScript? 将 object 传递给 function 时,如何指定 TypeScript 类型? - How can I specify a TypeScript type when passing an object into a function? 我如何在typescript中描述和调用object类型的function? - how can i describe and call a function of a type of object in typescript? 打字稿:类型“{}”不能指定为“选择”类型 <T, K> &#39;,如何正确输入javascript`pick`功能? - Typescript: Type '{}' is not assignable to type 'Pick<T, K>', how do I type javascript `pick` function correctly? 您将如何在 TypeScript 中创建通用工厂类型? - How would you create a generic Factory type in TypeScript? 如何在 TypeScript 中将函数参数类型标记为函数? - how to mark a function paramter Type as Function in TypeScript? 我如何用 ramda 编写该函数? - How i can compose that function with ramda? 当函数返回时,reduce 函数如何工作? 还想了解更多关于作曲和作曲的信息 - How does the reduce function work when a function is returned? Would also like to know more about compose and composition
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM