繁体   English   中英

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

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

我在我的转换Javascript库的同时,工艺组成风格的组件,以打字稿(什么我目前位于进行打字稿分支),但我有一个很难打字compose的功能(当然,有些其他可组合的功能也是如此,但现在......):

基本用法:

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

分解:

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" };

我目前拥有的:

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 };

标签无效:

 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 无效:

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

作为参考,这里是styled-components types

任何帮助将不胜感激。

我在我的转换Javascript库的同时,工艺组成风格的组件,以打字稿(什么我目前位于进行打字稿分支),但我有一个很难打字compose的功能(当然,有些其他可组合的功能也是如此,但现在......):

基本用法:

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

分解:

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" };

我目前拥有的:

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 };

标签无效:

 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 无效:

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

作为参考,这里是styled-components types

任何帮助将不胜感激。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM