Material-ui: withStyles()에 μ˜ν•΄ λ°˜ν™˜λœ ꡬ성 μš”μ†Œμ˜ Typescript μœ ν˜• 였λ₯˜

에 λ§Œλ“  2017λ…„ 09μ›” 28일  Β·  55μ½”λ©˜νŠΈ  Β·  좜처: mui-org/material-ui

typescriptμ—μ„œ withStyles() hocλ₯Ό μ‚¬μš©ν•  λ•Œ λ°˜ν™˜λœ ꡬ성 μš”μ†Œλ₯Ό μ‚¬μš©ν•˜λ €κ³  ν•  λ•Œ λ‹€μŒ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<App> & Readonly<{ children?: ReactNode; }> & Reado...'.
  Type '{}' is not assignable to type 'Readonly<WithStyles<"main">>'.
    Property 'classes' is missing in type '{}'.
  • [x] 이 μ €μž₯μ†Œμ˜ 문제 λ₯Ό κ²€μƒ‰ν–ˆμœΌλ©° 이것이 μ€‘λ³΅λ˜μ§€ μ•ŠλŠ”λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

μœ ν˜• μ •μ˜μ— λŒ€ν•œ μ΄λŸ¬ν•œ λ³€κ²½ 은 이 λ¬Έμ œμ™€ 관련이 μžˆμ„ 수 μžˆμŠ΅λ‹ˆλ‹€.

μ˜ˆμƒλ˜λŠ” λ™μž‘

μ•„λž˜μ˜ App ꡬ성 μš”μ†Œ μ½”λ“œκ°€ 주어지면 1.0.0-beta.10μ—μ„œ ν–ˆλ˜ κ²ƒμ²˜λŸΌ μœ ν˜• 였λ₯˜ 없이 ꡬ성 μš”μ†Œ <App /> λ₯Ό μ‚¬μš©ν•  수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

ν˜„μž¬ 행동

μ•„λž˜μ˜ App ꡬ성 μš”μ†Œ μ½”λ“œμ—μ„œ <App /> λ₯Ό μ‚¬μš©ν•˜λ €κ³  ν•˜λ©΄ μ•žμ„œ μ–ΈκΈ‰ν•œ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

μ½”λ“œ

import * as React from 'react';
import { withStyles } from 'material-ui/styles';

const styles = {
    main: {
        marginTop: 48,
        padding: 10,
    },
    foo: {
        margin: 0,
    },
};

interface Props {
    message: string;
};

type ClassNames = { classes: { [className in keyof typeof styles]: string } };

class App extends React.Component<Props & ClassNames> {
    render() {
        const { classes, message } = this.props;
        return (
            <div className={classes.main}>
                <div className={classes.foo} >
                    Hello World! {message}
                </div>
            </div>
        );
    }
}

export default withStyles(styles)(App);

λ¬Έλ§₯

μ½”λ“œλŠ” 1.0.0-beta.10μ—μ„œ μ œλŒ€λ‘œ μž‘λ™ν–ˆμ§€λ§Œ 1.0.0-beta.12둜 μ—…κ·Έλ ˆμ΄λ“œν–ˆμ„ λ•Œ μœ ν˜• 였λ₯˜κ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.

제곡된 μ½”λ“œ μ‘°κ°μ—μ„œ 클래슀 이름 λͺ©λ‘μ„ 두 번 μ •μ˜ν•  ν•„μš”κ°€ 없도둝 keyof typeof styles νŠΈλ¦­μ„ μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€(λ°˜λ³΅μ„±μ„ 맀우 μ‹«μ–΄ν•©λ‹ˆλ‹€). λ‚˜λŠ” λ˜ν•œ λ‹€λ₯Έ λ³€ν˜•μ„ μ‹œλ„ν–ˆμŠ΅λ‹ˆλ‹€:

type ClassNames = WithStyles<keyof typeof styles>;

더 일반적인 λ°©λ²•μœΌλ‘œ μˆ˜ν–‰ν•©λ‹ˆλ‹€( styles.spec.tsx μ—μ„œ λ³Ό 수

type ComponentClassNames = 'main' | 'foo';
type ClassNames = WithStyles<ComponentClassNames>;

μ—¬μ „νžˆ 같은 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.

이전 μœ ν˜• μ •μ˜λŠ” props μœ ν˜•μ΄ classes μ˜΅μ…˜μ΄ μžˆλŠ” StyledComponentProps 인 ꡬ성 μš”μ†Œλ₯Ό λ°˜ν™˜ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μƒˆλ‘œμš΄ μ •μ˜...

<P, C extends React.ComponentClass<P & StyledComponentProps<Names>>>(
    component: C
  ): C;

...ꡬ성 μš”μ†Œμ™€ λ™μΌν•œ μœ ν˜• C 을 λ°˜ν™˜ν•©λ‹ˆλ‹€. 즉, 선택 μ‚¬ν•­μœΌλ‘œ ν‘œμ‹œλ˜μ§€ μ•Šμ€ ClassNames λ₯Ό μ „λ‹¬ν•˜λ©΄ λ°˜ν™˜λœ ꡬ성 μš”μ†Œμ— μ „νŒŒλ©λ‹ˆλ‹€. λ‚΄κ°€ 언급을 μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€ 여기에 μ‚¬μš© Partial<> 내가보기 ν‰ν•œ ν•΄ν‚Ή μƒκ°ν•©λ‹ˆλ‹€.

λ‹Ήμ‹ μ˜ ν™˜κ²½

| 기술 | 버전 |
|----------------|---------|
| 머티리얼 UI | 1.0.0-베타.12 |
| λ°˜μ‘ | 15.6.1 |
| λΈŒλΌμš°μ € | 크둬 61.0.3163.100 |

discussion typescript

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

λ‚˜λŠ” μž¬κ΅¬μ„±μ„ μ‚¬μš©ν•˜μ—¬ ν•΄κ²°ν–ˆλ‹€.

μ˜ˆμ‹œ

import { StyleRules, Theme, withStyles } from "material-ui/styles";
import * as React from "react";
import { compose } from "recompose";

interface IProps {
    classes?: any; // <-- add question mark
    text: string;
}

const styles = (theme: Theme): StyleRules => ({
    root: {

    },
});

@compose(
    withStyles(styles),
)
export class MyComponent extends React.Component<IProps> {
    render() {
        const { classes, text } = this.props;
        return (
            <div className={classes.root}>
                {text}
            </div>
        );
    }
}

λͺ¨λ“  55 λŒ“κΈ€

@cfilipov 당신이 μ‚¬μš©ν•˜λŠ” ꡬ성 μš”μ†Œ 클래슀λ₯Όν•œλ‹€ μž₯식 ν•  λ•Œ StyledComponentProps λŒ€μ‹  WithStyles , 그리고 μ—„κ²©ν•œ νƒ€μž… 검사 λͺ¨λ“œμ—μ„œ 당신은 null이 μ•„λ‹Œ μ£Όμž₯ μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•  경우 ! μΆ”μΆœλ¬Όμ—μ„ classes 의 ν•„λ“œ 이것은 withStyles λ₯Ό 클래슀 λ°μ½”λ ˆμ΄ν„°λ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•˜λŠ” μ ˆμΆ©μ•ˆμž…λ‹ˆλ‹€. TypeScript의 클래슀 λ°μ½”λ ˆμ΄ν„°λŠ” λ°˜ν™˜ μœ ν˜•μ΄ 인수 μœ ν˜•κ³Ό μΌμΉ˜ν•΄μ•Ό ν•œλ‹€λŠ” μ œν•œ 이 μžˆμŠ΅λ‹ˆλ‹€. 이 μ œν•œ 사항이 μžˆλŠ” 경우 ν•œ 가지 μ˜΅μ…˜λ§Œ κ°€λŠ₯ν•©λ‹ˆλ‹€.

  1. withStyles λ₯Ό 클래슀 λ°μ½”λ ˆμ΄ν„°λ‘œ μ‚¬μš©ν•˜λŠ” 것을 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  2. μž₯μ‹λœ ꡬ성 μš”μ†Œ μœ ν˜•μ˜ μš”μ†Œλ₯Ό ꡬ성할 λ•Œ 더미 classes μ†Œν’ˆμ„ μ „λ‹¬ν•˜λ„λ‘ μš”κ΅¬ν•©λ‹ˆλ‹€(이전 μ ˆμΆ©μ•ˆμ€ 틀림없이 더 μ§œμ¦λ‚¬μŒ )
  3. classes κ°€ ꡬ성 μš”μ†Œ λ‚΄μ—μ„œ nullable둜 κ°„μ£Όλ˜λ„λ‘ μš”κ΅¬ν•˜μ—¬ ν•΄λ‹Ή ν•„λ“œμ— μ•‘μ„ΈμŠ€ν•  λ•Œ ! μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€( ν˜„μž¬ νƒ€ν˜‘ ).

ꡬ성 μš”μ†Œμ— μƒνƒœκ°€ μ—†λŠ” 경우 μƒνƒœ λΉ„μ €μž₯ κΈ°λŠ₯ ꡬ성 μš”μ†Œλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. 그러면 μœ ν˜• 주석이 더 적게 ν•„μš”ν•˜κ³  μœ ν˜•μ΄ 더 μ•ˆμ „ν•©λ‹ˆλ‹€.

export default withStyles(styles)<Props>(({ classes, message }) => (
  <div className={classes.main}>
    <div className={classes.foo}>
      Hello World! {message}
    </div>
  </div>
));

λ‚΄κ°€ 언급을 μ°Έμ‘°ν•˜μ‹­μ‹œμ˜€ 여기에 내가보기 ν‰ν•œ ν•΄ν‚Ή μƒκ°ν•˜λŠ” <> λΆ€λΆ„μ˜ μ‚¬μš©μ„.

ν•΄λ‹Ή λŒ“κΈ€μ— λŒ€ν•œ λ‚΄ 후속 쑰치λ₯Ό 읽으면 Partial<> κ°€ ν•„μš”ν•˜μ§€ μ•Šλ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

ν•΄λ‹Ή λŒ“κΈ€μ— λŒ€ν•œ λ‚΄ 후속 쑰치λ₯Ό 읽으면 Partial<>이 ν•„μš”ν•˜μ§€ μ•Šλ‹€λŠ” 것을 μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

Partial<> 에 λŒ€ν•œ κ·€ν•˜μ˜ μ˜κ²¬μ„ λ³΄μ•˜μŠ΅λ‹ˆλ‹€. StyledComponentProps λ₯Ό μ‚¬μš©ν•˜λŠ” 것은 본질적으둜 λ™μΌν•œ 것과 κ°™μŠ΅λ‹ˆλ‹€(ν•΄λ‹Ή μ •μ˜μ—μ„œ Partial<> ν•˜κ³  있음). λ‚΄ λΆˆλ§Œμ€ 이제 클래슀 이름에 ! 둜 μ•‘μ„ΈμŠ€ν•΄μ•Ό ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€(당신이 μ–ΈκΈ‰ν–ˆλ“―μ΄). 더미 classes μ†Œν’ˆμ„ μ „λ‹¬ν•˜κ±°λ‚˜ ! μ‚¬μš©ν•˜λ„λ‘ μš”κ΅¬ν•˜λŠ” 것은 λ‘˜ λ‹€ 쒋지 μ•Šμ€ μ ˆμΆ©μ•ˆμ΄λΌκ³  μƒκ°ν•©λ‹ˆλ‹€.

λͺ…ν™•νžˆ ν•˜μžλ©΄, μ—¬κΈ°μ„œ μ œκ°€ κ²ͺκ³  μžˆλŠ” 근본적인 λ¬Έμ œλŠ” typescript의 μ‹€ν—˜μ  κΈ°λŠ₯ 의 ν•œκ³„λ₯Ό ν•΄κ²°ν•˜κΈ° μœ„ν•œ λ…Έλ ₯으둜 μž„μ‹œμ— νšŒκ·€λ₯Ό λ„μž…ν•˜λŠ” 선택이라고 μƒκ°ν•©λ‹ˆλ‹€. λ‚˜λŠ” λ°μ½”λ ˆμ΄ν„°μ— 관심이 μ—†κΈ° λ•Œλ¬Έμ— μ•½κ°„μ˜ νŽΈκ²¬μ„ μΈμ •ν•˜μ§€λ§Œ λ‹€λ₯Έ μ‚¬λžŒλ“€μ€ λΆ„λͺ…νžˆ μ‹ κ²½ μ“°μ§€λ§Œ μ—¬κΈ°μ—μ„œμ˜ μ ˆμΆ©μ•ˆμ€ λ‚˜μ—κ²Œ μ˜λ―Έκ°€ μ—†μŠ΅λ‹ˆλ‹€.

@cfilipov withStyles νƒ€μ΄ν•‘μ˜ 첫 번째 λ¦¬νŒ©ν„° λŠ” μ˜΅μ…˜ (1)을 μ„ νƒν•˜λŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€. 즉, withStyles λ₯Ό 클래슀 λ°μ½”λ ˆμ΄ν„°λ‘œ μ‚¬μš©ν•˜λŠ” λŒ€μ‹  클래슀 및 ν•¨μˆ˜ ꡬ성 μš”μ†Œ λͺ¨λ‘μ— λŒ€ν•΄ 타이핑을 μ™„μ „νžˆ μ˜¬λ°”λ₯΄κ²Œ λ§Œλ“œλŠ” κ²ƒμž…λ‹ˆλ‹€. . 그런 λ‹€μŒ λ°μ½”λ ˆμ΄ν„° ꡬ문을 μ‚¬μš©ν•˜λŠ” 것이 인기 μžˆλŠ” μš”μ²­μ΄λΌλŠ” ν”Όλ“œλ°± 을 λ°›μ•„ μ˜΅μ…˜ (3)으둜 μ „ν™˜ν–ˆμŠ΅λ‹ˆλ‹€. κ·Έ 결정을 λ‹€μ‹œ 보게 λ˜μ–΄ κΈ°μ©λ‹ˆλ‹€. λ‚˜λ„ ν˜„μž¬ 반기λŠ₯ μƒνƒœμ—μ„œ λ°μ½”λ ˆμ΄ν„°λ₯Ό μ§€μ›ν•˜λŠ” 것보닀 μœ ν˜• μ•ˆμ „μ„±μ„ μ„ ν˜Έν•©λ‹ˆλ‹€.

λ„€, 이런 인기 μžˆλŠ” μš”μ²­μ„ μ§€μ›ν•˜κ³  싢은 λ§ˆμŒμ€ μ΄ν•΄ν•©λ‹ˆλ‹€. λ‚˜λ„ λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš© ν•˜κ³  μ‹Άμ§€λ§Œ (mui μ™ΈλΆ€) ν•  λ•Œλ§ˆλ‹€ λ°μ½”λ ˆμ΄ν„°μ™€ κ΄€λ ¨λœ λ§Žμ€ λ¬Έμ œκ°€ 계속 λ°œμƒν•˜λ―€λ‘œ 개인적으둜 쀀비될 λ•ŒκΉŒμ§€ μ‚¬μš©ν•˜μ§€ μ•ŠκΈ°λ‘œ κ²°μ •ν–ˆμŠ΅λ‹ˆλ‹€.

당신이 λ‚΄ 관심사λ₯Ό κ³΅μœ ν•˜λŠ” κ²ƒμ²˜λŸΌ 듀리고 더 이상 μΆ”κ°€ν•  것이 μ—†μœΌλ―€λ‘œ λ‹€λ₯Έ μ‚¬λžŒλ“€μ΄ 이 λ°©ν–₯으둜 ν”Όλ“œλ°±μ„ μ œκ³΅ν•˜μ—¬ 영ν–₯을 쀄 수 있기λ₯Ό λ°”λžλ‹ˆλ‹€.

λ‚˜λŠ” beta.10μ—μ„œ beta.13으둜 μ „ν™˜ν•˜μ—¬ λ³€κ²½λœ 사항이 μžˆλŠ”μ§€ ν™•μΈν–ˆμœΌλ©° 예, 이것은 μ‹€μ œ λ¬Έμ œμž…λ‹ˆλ‹€. 여기에 λ‚΄ 2μ„ΌνŠΈλ₯Ό λ˜μ§€κΈ° μœ„ν•΄ λ°μ½”λ ˆμ΄ν„°λŠ” μ‹€ν—˜μ μž…λ‹ˆλ‹€. 그듀은 λΆ„λͺ…νžˆ λ―Έλž˜μ— λ°”λ€” 수 μžˆμŠ΅λ‹ˆλ‹€. 그리고 κ·Έλ•ŒκΉŒμ§€ 100% μ •ν™•ν•œ 방법을 μ „μ μœΌλ‘œ μ§€μ§€ν•©λ‹ˆλ‹€. λ‚˜λŠ” 일을 μž‘λ™μ‹œν‚€κΈ° μœ„ν•΄ λ‚΄ μœ ν˜•μ„ ν•΄ν‚Ήν•˜λŠ” 데 일관성 μžˆλŠ” μœ ν˜• μ•ˆμ „μ„ 훨씬 더 μ„ ν˜Έν•©λ‹ˆλ‹€.

8550은 μ‚¬λžŒλ“€μ΄ 이것에 λŒ€ν•΄ ν˜Όλž€μŠ€λŸ¬μ›Œν•œλ‹€λŠ” μΆ”κ°€ 증거처럼 보이며, μš°λ¦¬λŠ” @withStyles() λ₯Ό (TypeScriptμ—μ„œ) λ°μ½”λ ˆμ΄ν„°λ‘œ μ§€μ›ν•˜μ§€ μ•ŠλŠ” 것을 κ³ λ €ν•΄μ•Ό ν•©λ‹ˆλ‹€.

타이핑을 μ˜¬λ°”λ₯΄κ²Œ ν–ˆλ‹€λ©΄ 클래슀λ₯Ό "μž₯식"ν•˜λŠ” λͺ¨μŠ΅μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

type NonStyleProps = {
  text: string
};

const styles = {
  root: {
    backgroundColor: 'red'
  }
};

const DecoratedComponent = withStyles(styles)(
  class extends React.Component<NonStyleProps & WithStyles<'root'>> {
    render() {
      return (
        <div className={this.props.classes.root}>
          {this.props.text}
        </div>
      );
    }
  }
);

@pelotom 이 문제 λ•Œλ¬Έμ— 아직 beta.10μ—μ„œ μ•žμœΌλ‘œ λ‚˜μ•„κ°€μ§€ λͺ»ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€. 이전에 Redux의 connect λ©”μ†Œλ“œλ₯Ό μ‚¬μš©ν•œ 클래슀 μŠ€νƒ€μΌμ— λŒ€ν•΄ μ–ΈκΈ‰ν•œ 적이 μžˆμŠ΅λ‹ˆλ‹€. μƒλŒ€μ μœΌλ‘œ 쉽고 κ°•λ ₯ν•˜λ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€. #8059μ—μ„œ μ„Έ 번째 주석은 μœ ν˜•μ— λŒ€ν•œ λ‚˜ μžμ‹ μž…λ‹ˆλ‹€.

@pelotom 이 λ¬Έμ œμ— λŒ€ν•΄ μ•Œλ €μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€. μ €λŠ” TS λ°μ½”λ ˆμ΄ν„°λ₯Ό 많이 μ‚¬μš©ν•˜μ§€λ§Œ, 그런 κ²½μš°μ—λŠ” withStyles κ°€ 더 λ‚˜μ€ μœ ν˜• μ•ˆμ „μ„±μ„ μœ„ν•΄ λ°μ½”λ ˆμ΄νŒ… 지원을 μ€‘λ‹¨ν•˜κ²Œ λ˜μ–΄ κΈ°μ©λ‹ˆλ‹€.

@withStyle()을 (TypeScriptμ—μ„œ) λ°μ½”λ ˆμ΄ν„°λ‘œ μ§€μ›ν•˜μ§€ μ•ŠλŠ” 것을 κ³ λ €ν•΄μ•Ό ν•©λ‹ˆλ‹€.

@pelotom μ €λŠ” 개인적으둜 이 변화에 μ°¬μ„±ν•©λ‹ˆλ‹€. @sebald μ—¬κΈ°μ„œ 무엇을 ν•˜κ³ 

κ°„λ‹¨ν•œ λ³€κ²½μž…λ‹ˆλ‹€. ν˜Ήμ‹œλΌλ„ ν•¨κ»˜ κ°€μ‹€ 뢄듀을 μœ„ν•΄ PR을 μ—΄μ—ˆμ–΄μš” πŸ™‚

@pelotom μ΄λ ‡κ²Œ μ‚¬μš©ν•˜λ©΄ 타이핑이 계속 μž‘λ™ν• κΉŒμš”?

interface IStyles {
    // styles interface
}
interface IHeaderInfoStyles {
    classes: any;
}
interface IHeaderInfoProps {
    // properties resulting from dispatches
}
interface IHeaderInfoInjectedProps {
   // props injected from parent if any
}
interface IHeaderInfoDispatchProps {
    // dispatches
}
interface IHeaderInfoState {
    // your state for the class
}

type HeaderInfoProps = IHeaderInfoProps & IHeaderInfoInjectedProps & IHeaderInfoDispatchProps & IHeaderInfoStyles;

class HeaderInfo extends Component<HeaderInfoProps, IHeaderInfoState> {

export const HeaderInfo_container = withStyles<IHeaderInfoInjectedProps, IStyles>(styles)(
    connect<IHeaderInfoProps, IHeaderInfoDispatchProps, (IHeaderInfoInjectedProps & IHeaderInfoStyles)>(mapStateToProps, mapDispatchToProps)(HeaderInfo),
);

@marcusjwhelan withStyles λŠ” 더 이상 2개의 μœ ν˜• λ§€κ°œλ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠμœΌλ©° μŠ€νƒ€μΌμ— λŒ€ν•œ μœ ν˜• λ§€κ°œλ³€μˆ˜λ₯Ό μ œκ³΅ν•  ν•„μš”κ°€ μ—†μŠ΅λ‹ˆλ‹€( styles μ—μ„œ μœ μΆ”ν•  수 있음). λŒ€μ‹  μ“Έ 수 μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€.

withStyles(styles)<NonStyleProps>(...);

mapStateToProps 및 mapDispatchToProps λŒ€ν•œ μœ ν˜•μ„ μ œκ³΅ν•˜λ©΄ κ·€ν•˜μ˜ μ˜ˆμ—μ„œ 이것이 μ–΄λ–»κ²Œ λ³΄μ΄λŠ”μ§€ 보여쀄 수 μžˆμŠ΅λ‹ˆλ‹€.

헐; 박사; 변경을 ν•˜κ³  반발이 어떀지 λ΄…μ‹œλ‹€ πŸ‘·πŸ»


@oliviertassinari @pelotom Β―_(ツ)_/Β― μ €λŠ” λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 개인적으둜 이 변경에 λŒ€ν•΄ λ³„λ‘œ μ‹ κ²½ 쓰지 μ•ŠμŠ΅λ‹ˆλ‹€. 영ν–₯을 받지 μ•ŠκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. ν•˜μ§€λ§Œ λ§Žμ€ μ‚¬λžŒλ“€μ΄ 이 "κΈ°λŠ₯"에 신경을 μ“°λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. 그것이 μš°λ¦¬κ°€ μ²˜μŒμ— 그것을 μΆ”κ°€ν•œ μ΄μœ μž…λ‹ˆλ‹€. μ—¬κΈ°μ—μ„œ μš°μ„  μˆœμœ„κ°€ 높은 것은 IMHOμž…λ‹ˆλ‹€.

withStyles 의 λ³€κ²½ 사항에 맀우 λ§Œμ‘±ν•˜μ§€λ§Œ ramda λ˜λŠ” recompose 와 같은 더 κΈ°λŠ₯적인 λ‹€λ₯Έ 라이브러리λ₯Ό μ‚΄νŽ΄λ³΄λ©΄ 타이핑이 κ·Έλ ‡κ²Œ μ—„κ²©ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‘˜ λ‹€ μœ ν˜• μ•ˆμ „ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ’…μ’… ν•¨μˆ˜μ˜ λ°˜ν™˜ 값을 λ‚˜νƒ€λ‚΄λŠ” μ œλ„€λ¦­μ„ 전달해야 ν•©λ‹ˆλ‹€. μ˜ˆμ˜μ§€λŠ” μ•Šμ§€λ§Œ 99.9%의 μ‚¬μš©μžμ—κ²Œ μ ν•©ν•©λ‹ˆλ‹€.

λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜λŠ” μ‚¬λžŒλ“€μ—κ²Œ νš¨κ³Όκ°€ μžˆμ—ˆλ˜ 였래된 타이핑을 λ‹€μ‹œ κ°€μ Έμ˜¬ 수 μ—†λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? 두 개의 μ œλ„€λ¦­μ΄ 있기 λ•Œλ¬Έμ— 두 μž…λ ₯을 λ‚˜λž€νžˆ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

λ˜ν•œ λ°μ½”λ ˆμ΄ν„°μ™€ κ΄€λ ¨λœ μ‚¬λžŒλ“€μ˜ λ¬Έμ œκ°€ 무엇인지 μ•½κ°„ ν˜Όλž€μŠ€λŸ½μŠ΅λ‹ˆλ‹€("μž‘λ™ν•˜μ§€ μ•ŠμŒ" κ΄€λ ¨). Angular 및 https://nestjs.com/ κ³Ό 같은 λΌμ΄λΈŒλŸ¬λ¦¬λŠ” κ²°κ΅­ 이λ₯Ό 많이 μ‚¬μš©ν•©λ‹ˆλ‹€. 우리의 경우 μ‚¬λžŒλ“€μ€ μ†Œν’ˆμ— WithStyles λ₯Ό μΆ”κ°€ν•˜λ©΄ λ©λ‹ˆλ‹€.

@sebald

λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜λŠ” μ‚¬λžŒλ“€μ—κ²Œ νš¨κ³Όκ°€ μžˆμ—ˆλ˜ 였래된 타이핑을 λ‹€μ‹œ κ°€μ Έμ˜¬ 수 μ—†λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? 두 개의 μ œλ„€λ¦­μ΄ 있기 λ•Œλ¬Έμ— 두 μž…λ ₯을 λ‚˜λž€νžˆ κ°€μ§ˆ 수 μžˆμŠ΅λ‹ˆλ‹€.

무슨 말씀이신지 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€. μ‚¬λžŒλ“€μ΄ λ°μ½”λ ˆμ΄ν„°λ₯Ό 고톡 없이 μ‚¬μš©ν•  수 μžˆλŠ” 타이핑은 μ—†μ—ˆμŠ΅λ‹ˆλ‹€. 첫 번째 κ΅¬ν˜„μ€ 더미 classes μ†Œν’ˆ(예: <StyledComp classes={{} as any} /> 을 μ „λ‹¬ν•˜μ§€ μ•Šκ³  μž₯μ‹λœ ꡬ성 μš”μ†Œμ˜ μš”μ†Œλ₯Ό ꡬ성할 수 μ—†λ‹€λŠ” #8267둜 인해 어렀움을 κ²ͺμ—ˆμŠ΅λ‹ˆλ‹€. 두 번째(ν˜„μž¬) κ΅¬ν˜„μ€ classes κ°€ ꡬ성 μš”μ†Œ 클래슀 λ‚΄μ—μ„œ 잠재적으둜 undefined 둜 κ°„μ£Όλ˜λŠ” λ°˜λŒ€μ˜ 문제둜 어렀움을 κ²ͺμŠ΅λ‹ˆλ‹€. ν˜„μž¬ ν˜•μ‹μœΌλ‘œ TypeScript λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‚¬μš©ν•˜λ €λ©΄ 이 두 가지 μ˜΅μ…˜λ§Œ 있으면 λ©λ‹ˆλ‹€. λ‹Ήμ‹ μ˜ 독을 μ„ νƒν•˜μ‹­μ‹œμ˜€.

μ„Έ 번째 방법은 μ˜¬λ°”λ₯Έ μœ ν˜•μ„ μ‚¬μš©ν•˜μ—¬ classes κ°€ 클래슀 λ‚΄μ—μ„œ μ •μ˜λ˜μ§€λ§Œ μ†Œν’ˆμœΌλ‘œ 전달할 ν•„μš”κ°€ 없도둝 ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€. 이 두 가지 쑰건을 λͺ¨λ‘ κ°–μΆ”λ €λ©΄ λ°μ½”λ ˆμ΄ν„°λ₯Ό 포기해야 ν•©λ‹ˆλ‹€.

@pelotom λ„€, μ£„μ†‘ν•©λ‹ˆλ‹€. λ„€κ°€ μ˜³μ•„. μ§„μ§œ 내날은 μ•„λ‹Œλ°... 🀐 그럼 λ³‘ν•©ν•˜μž!

@sebald

λ˜ν•œ λ°μ½”λ ˆμ΄ν„°μ™€ κ΄€λ ¨λœ μ‚¬λžŒλ“€μ˜ λ¬Έμ œκ°€ 무엇인지 μ•½κ°„ ν˜Όλž€μŠ€λŸ½μŠ΅λ‹ˆλ‹€("μž‘λ™ν•˜μ§€ μ•ŠμŒ" κ΄€λ ¨). Angular 및 https://nestjs.com/ κ³Ό 같은 λΌμ΄λΈŒλŸ¬λ¦¬λŠ” κ²°κ΅­ 이λ₯Ό 많이 μ‚¬μš©ν•©λ‹ˆλ‹€.

Angularμ—μ„œ μ–΄λ–»κ²Œ μ‚¬μš©λ˜λŠ”μ§€ 잘 λͺ¨λ₯΄κ² μ§€λ§Œ λ°μ½”λ ˆμ΄ν„°λ₯Ό μ‰½κ²Œ μ‚¬μš©ν•  수 μžˆλŠ” μ‚¬μš© μ‚¬λ‘€λŠ” λΆ„λͺ…νžˆ μžˆμŠ΅λ‹ˆλ‹€. 기본적으둜 λ°μ½”λ ˆμ΄ν„°κ°€ κΎΈλ―Έκ³  μžˆλŠ” 클래슀의 μœ ν˜•μ„ λ³€κ²½ν•  ν•„μš”κ°€ μ—†λ‹€λ©΄ μ œλŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€. μš°λ¦¬κ°€ μ²˜ν•œ 상황은 그렇지 μ•ŠμŠ΅λ‹ˆλ‹€. withStyles λŠ” μž₯μ‹ν•˜λŠ” ꡬ성 μš”μ†Œμ˜ μœ ν˜•μ„ λ³€κ²½ν•΄μ•Ό ν•©λ‹ˆλ‹€.

@pelotom λ„€, λ§žμŠ΅λ‹ˆλ‹€. λ‚˜μœ 것은 λŒμ—°λ³€μ΄ μΌλΏμž…λ‹ˆλ‹€. μ‹€μ œλ‘œ, TSκ°€ ν˜„μž¬ λ°μ½”λ ˆμ΄ν„°λ₯Ό κ΅¬ν˜„ν•˜λŠ” 방식은 Angular μ‚¬μš©μžμ—κ²Œλ„ 쒋을 수 μžˆμŠ΅λ‹ˆλ‹€. λ°μ½”λ ˆμ΄ν„° AFAIKλŠ” "이 클래슀λ₯Ό ꡬ성 μš”μ†Œλ‘œ 등둝" λ˜λŠ” "DIμ—μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ 메타 데이터 μΆ”κ°€"와 같이 ν”„λ ˆμž„μ›Œν¬μ™€ 계약을 μ„€μ •ν•˜κΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. " ... 신이 λ‚˜μ„œ μ“°κΈ°λ§Œ 해도 기뢄이 μ’‹μ•„μ§€λ„€μš” 🀒

@pelotom λ‚΄κ°€ν•˜λŠ” λ°©μ‹μœΌλ‘œ μœ ν˜•μ„ μ‚¬μš©ν•˜λŠ” μ΄μœ λŠ” λ‚΄ ꡬ성 μš”μ†Œμ— μœ ν˜• μ•ˆμ „μ„±μ„ μ œκ³΅ν–ˆκΈ° λ•Œλ¬Έμž…λ‹ˆλ‹€. ν˜„μž¬ μœ ν˜•μ—λŠ” ꡬ성 μš”μ†Œμ™€ κ΄€λ ¨ν•˜μ—¬ μœ ν˜• μ•ˆμ „μ΄ μ—†μŠ΅λ‹ˆλ‹€. λ‚΄ 예제의 injectionProps λŠ” ꡬ성 μš”μ†Œκ°€ μž‘λ™ν•˜λŠ” 데 ν•„μš”ν•œ μœ ν˜•μž…λ‹ˆλ‹€. react-redux μ—°κ²° μœ ν˜•μ€ mapStateToProps μ—μ„œ μ˜€λŠ” Props 및 mapDispatchToProps μ—μ„œ μ˜€λŠ” DispatchProps 의 μœ ν˜•μ΄μ–΄μ•Ό ν•©λ‹ˆλ‹€.

맨 λμ—μ„œ λ‚΄ λΆ€λͺ¨ ꡬ성 μš”μ†Œκ°€ λ‚΄κ°€ μ‚½μž…ν•΄μ•Ό ν•˜κ±°λ‚˜ λ‚΄ ν”„λ‘œμ νŠΈκ°€ μ»΄νŒŒμΌλ˜μ§€ μ•ŠλŠ”λ‹€λŠ” 것을 μ•Œ 수 μžˆλ„λ‘ injectionProps κ°€ μžˆμ–΄μ•Ό ν•©λ‹ˆλ‹€. (이것이 λ‚΄κ°€ μ›ν•˜λŠ”κ±°μ•Ό). 이 λ³€κ²½ 사항이 μ΄λŸ¬ν•œ μ’…λ₯˜μ˜ typeSafety둜 λ‹€μ‹œ λ§ˆμ΄κ·Έλ ˆμ΄μ…˜λ©λ‹ˆκΉŒ?

@marcusjwhelan λ‹€μ‹œ, 베타 10μ—μ„œ 컴파일된 전체(κ·ΈλŸ¬λ‚˜ μ΅œμ†Œν•œμ˜) 예제λ₯Ό μ œκ³΅ν•  수 μžˆλ‹€λ©΄ λ§ˆμ΄κ·Έλ ˆμ΄μ…˜ 방법을 λ³΄μ—¬λ“œλ¦΄ 수 μžˆμŠ΅λ‹ˆλ‹€. AFAIK μƒˆ 버전은 이전보닀 λͺ¨λ“  λΆ€λΆ„μ—μ„œ ν‘œν˜„λ ₯이 ν’λΆ€ν•˜κ³  μœ ν˜•μ΄ μ•ˆμ „ν•΄μ•Ό ν•©λ‹ˆλ‹€.

@pelotom 어리석은 질문, μƒˆ λ¦΄λ¦¬μŠ€κ°€ μ™„λ£Œλ˜λ©΄ μ•Œλ¦Όμ„ 받을 수 μžˆλŠ” 방법이 μžˆμŠ΅λ‹ˆκΉŒ?

@wcandillon νŠΈμœ„ν„°μ—μ„œ νŒ”λ‘œμš°ν•˜μ„Έμš”.

@pelotom μœ„μ— μ˜ˆμ‹œλ₯Ό

νŽΈμ§‘ν•˜λ‹€

λ‚˜λŠ” 그것을 μ•Œμ•„.

이 토둠을 쑰금 늦게 μ‹œμž‘ν–ˆμ§€λ§Œ μ—¬κΈ°μ—μ„œ μ–ΈκΈ‰ν•œ 것과 μœ μ‚¬ν•œ 였λ₯˜κ°€ λ°œμƒν–ˆμœΌλ©° 이λ₯Ό μˆ˜μ •ν•  수 μžˆλŠ” 방법을 아직 κ²°μ •ν•˜μ§€ λͺ»ν–ˆμŠ΅λ‹ˆλ‹€. [email protected] 및 [email protected] μžˆμŠ΅λ‹ˆλ‹€.

const LoginForm = withStyles(styles)(
    class extends React.Component<WithStyles<'root' | 'button'>, ILoginFormState> {
    // ...
    }
);

render(
  <MuiThemeProvider theme={theme}>
    <LoginForm />
  </MuiThemeProvider>,
  document.getElementById('login-form')
);

이 νŠΉλ³„ν•œ κ²½μš°μ— λ‚˜νƒ€λ‚˜λŠ” 였λ₯˜ λ©”μ‹œμ§€λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

Type '{}' is not assignable to type 'IntrinsicAttributes & WithStyles<"root" | "button"> & StyledComponentP...'.
  Type '{}' is not assignable to type 'WithStyles<"root" | "button" | "progress" | "textField">'.
    Property 'classes' is missing in type '{}'.
 not assignable to type 'WithStyles<"root" | "button" | "progress" | "textField">'.
    Property 'classes' is missing in type '{}'.

ꡬ성 μš”μ†Œ 클래슀λ₯Ό μž₯식할 λ•Œ WithStyles λŒ€μ‹  StyledComponentPropsλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

@pelotom - μ–΄λ”˜κ°€μ— μ΄κ²ƒμ˜ μ˜ˆκ°€ μžˆμŠ΅λ‹ˆκΉŒ? TypeScriptμ—μ„œ μƒνƒœ μ €μž₯ ꡬ성 μš”μ†Œ 클래슀의 μŠ€νƒ€μΌμ„ μ§€μ •ν•˜λŠ” 방법을 κ²°μ •ν•˜λŠ” 데 λ§Žμ€ λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€.

@iamjem 당신이 μ œκ³΅λ˜μ§€ μ•Šμ•˜κΈ° λ•Œλ¬Έμ— λ§ν•˜λŠ” 건 μ’€ μ–΄λ ΅λ‹€ styles ,ν•˜μ§€λ§Œ 첫 번째 λ¬Έμ œλŠ” 당신이 μ–ΈκΈ‰ ν•œ 더 클래슀 ν‚€λ₯Ό κ°€μ§€κ³ μžˆλ‹€ 것 κ°™μŠ΅λ‹ˆλ‹€ styles 당신이 μ–ΈκΈ‰ν•˜λŠ” 것보닀 WithStyles<...> . 둜 λ°”κΎΈμ‹œλ©΄ 될 것 κ°™μ•„μš”

const LoginForm = withStyles(styles)(
    class extends React.Component<WithStyles<keyof typeof styles>, ILoginFormState> {
    // ...
    }
);

첫 번째 문제λ₯Ό μ²˜λ¦¬ν•΄μ•Ό ν•©λ‹ˆλ‹€. 그런 λ‹€μŒ 두 번째 λ¬Έμ œκ°€ μžˆλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. LoginForm 의 κ²°κ³Ό μœ ν˜•μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

React.ComponentType<WithStyles<"root" | "button"> & StyledComponentProps<"root" | "button">>

이것은 λΆ„λͺ…νžˆ μ˜³μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. non-style props의 λΆ€μž¬κ°€ νƒ€μž… μ‹œμŠ€ν…œμ„ ν˜Όλž€μŠ€λŸ½κ²Œ ν•˜λŠ” κ²ƒμ²˜λŸΌ λ³΄μž…λ‹ˆλ‹€. {} λ₯Ό μœ ν˜• 인수둜 μ „λ‹¬ν•˜μ—¬ μŠ€νƒ€μΌμ΄ μ•„λ‹Œ propsκ°€ 무엇인지 λͺ…μ‹œμ μœΌλ‘œ μ„€λͺ…ν•˜λ©΄ 도움이 될 수 μžˆμŠ΅λ‹ˆλ‹€.

const LoginForm = withStyles(styles)<{}>( // <-- note the type argument
    class extends React.Component<WithStyles<keyof typeof styles>, ILoginFormState> {
    // ...
    }
);

λ„ˆλ¬΄ λ³΅μž‘ν•΄μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€. 이 μž‘μ—…μ„ 더 μ‰½κ²Œ μˆ˜ν–‰ν•  수 μžˆλŠ” 방법을 μ•Œκ³  μ‹ΆμŠ΅λ‹ˆλ‹€!

κ·Έ νŠΈλ¦­μ„ ν–ˆλ‹€! λΉ λ₯Έ 도움을 μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€ @pelotom. λ‚˜λŠ” μ˜€λž«λ™μ•ˆ Reactλ₯Ό μ‚¬μš©ν•΄ μ™”μ§€λ§Œ μ΅œκ·Όμ— Material-ui에 λ›°μ–΄λ“€μ—ˆκ³ , μ‚¬μš©ν•˜λŠ” λ™μ•ˆ Typescriptλ₯Ό μ‚¬μš©ν•΄ 보고 μ‹Άλ‹€κ³  μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. 말할 ν•„μš”λ„ 없이 Typescriptλ₯Ό ν–‰λ³΅ν•˜κ²Œ λ§Œλ“œλŠ” 방법을 λ§ν•˜κΈ° μ–΄λ €μš΄ λͺ‡ 가지 극단적인 κ²½μš°κ°€ μžˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” μž¬κ΅¬μ„±μ„ μ‚¬μš©ν•˜μ—¬ ν•΄κ²°ν–ˆλ‹€.

μ˜ˆμ‹œ

import { StyleRules, Theme, withStyles } from "material-ui/styles";
import * as React from "react";
import { compose } from "recompose";

interface IProps {
    classes?: any; // <-- add question mark
    text: string;
}

const styles = (theme: Theme): StyleRules => ({
    root: {

    },
});

@compose(
    withStyles(styles),
)
export class MyComponent extends React.Component<IProps> {
    render() {
        const { classes, text } = this.props;
        return (
            <div className={classes.root}>
                {text}
            </div>
        );
    }
}

λ‚˜λŠ” μ§€λ‚œ λ©°μΉ  λ™μ•ˆ λͺ…ν™•ν•œ κ²°κ³Ό 없이 이 문제(및 #8704)둜 μ‹Έμš°κ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€. 그런 λ‹€μŒ @pelotom 의 쑰언을

당신은 μ‚¬μš©ν•΄μ•Ό StyledComponentProps λŒ€μ‹  WithStyles

이 문제λ₯Ό ν•΄κ²°ν•˜λŠ” μœ μ‚¬ν•œ 방법을 GitHubμ—μ„œ κ²€μƒ‰ν–ˆμŠ΅λ‹ˆλ‹€. 그리고 ν•˜λ‚˜μ˜ μž‘λ™ 예λ₯Ό μ°Ύμ•˜μŠ΅λ‹ˆλ‹€ πŸ˜‚. κ·Έλž˜λ„ 쒋은 방법이고 λ‚΄ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€. TypeScriptκ°€ ν–‰λ³΅ν•œ 쿠킀인 λ³„λ„μ˜ μ»¨ν…Œμ΄λ„ˆμ™€ ꡬ성 μš”μ†Œ: 여기에 μ–ΈκΈ‰λœ 예 ( μ°Έκ³ : 제 경우 ꡬ성 μš”μ†Œ 맀핑은 λ³„λ„μ˜ μ»¨ν…Œμ΄λ„ˆ νŒŒμΌμ— μžˆμ§€λ§Œ μ•„μ΄λ””μ–΄λŠ” 같은.).

λˆ„κ΅°κ°€ 이것이 λ‚˜μœ 해결책이라고 μƒκ°ν•œλ‹€λ©΄ λ‚˜λŠ” μ–΄λ–€ 변화와 아이디어에도 μ—΄λ € μžˆμŠ΅λ‹ˆλ‹€. μ§€κΈˆμ€ λ‚΄ μ½”λ“œκ°€ λΆˆν‰μ„ 멈좘 것 κ°™μ•„ κΈ°μ©λ‹ˆλ‹€.

type Styles = 'foo';
const styles: StyleRulesCallback<Styles> = (theme: Theme) => ({
    foo: {
        position: 'relative',
    }
});

interface Props {
  something: string;
}

class Sidebar extends React.Component<Props & WithStyles<Styles>> {
    render() {
        const { classes, something } = this.props;

        return (
                    <div className={classes.foo}>{something}</div>
        );
    }
}

export default withStyles(styles)<Props>(Sidebar);

μƒˆ 문제λ₯Ό λ§Œλ“€κ³  μ‹Άμ§€λŠ” μ•Šμ§€λ§Œ λ¬Έμ„œ, μ˜ˆμ—μ„œ λ³Έ λͺ¨λ“  것을 μ‹œλ„ν•˜κ³  μž¬κ΅¬μ„±μ„ μ‚¬μš©ν•˜μ—¬ 문제λ₯Ό μ „λ‹¬ν–ˆμ§€λ§Œ 일뢀 속성을 μΆ”κ°€ν•˜λ©΄ ꡬ성 μš”μ†Œκ°€ μž‘λ™ν•˜λ„λ‘ λ§Œλ“€ 수 μ—†μŠ΅λ‹ˆλ‹€.
그리고 λ‚΄κ°€ 찾은 λ¦¬μ†ŒμŠ€λŠ” λŒ€λΆ€λΆ„ 이전 λ²„μ „μ˜ TS, MUI λ˜λŠ” React와 ν•¨κ»˜ μžˆμŠ΅λ‹ˆλ‹€.

λ‚΄ ꡬ성 μš”μ†ŒλŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

import React from 'react';
import AppBar from 'material-ui/AppBar';
import { withStyles, WithStyles, StyleRulesCallback } from 'material-ui/styles';

const styles: StyleRulesCallback<'positionFixed'> = () => ({
  positionFixed: {
    top: 'auto',
    bottom: 0,
  },
});

const decorate = withStyles(styles);

interface Props {
   someProp: string;
};

export const BottomNav = decorate<Props>(
  class extends React.Component<Props & WithStyles<'positionFixed'>, {}> {
    render() {
      return (
        <AppBar />
      );
    }
  }
);

export default BottomNav;

그리고 였λ₯˜λŠ” λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

TS2322: Type '{}' is not assignable to type 'IntrinsicAttributes & Props & StyledComponentProps<"positionFixed"> & { children?: ReactNode; }'.
  Type '{}' is not assignable to type 'Props'.
    Property 'someProp' is missing in type '{}'.

μ €λŠ” TS의 μ΄ˆλ³΄μžμ΄μ§€λ§Œ μ„€λͺ…μ„œ νŽ˜μ΄μ§€ 와 μ˜ˆμ œκ°€ 맀우 ν˜Όλž€μŠ€λŸ½κ±°λ‚˜ μ™„μ „ν•˜μ§€ μ•Šλ‹€λŠ” 것을 μ•Œμ•˜μŠ΅λ‹ˆλ‹€.

ν˜Ήμ‹œ μ•„μ‹œλŠ”λΆ„ κ³„μ‹œλ©΄ μ•Œλ €μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€ ;-)

@otroboe μ‹€μ œλ‘œ 였λ₯˜ ν”Œλž˜κ·Έκ°€ μ§€μ •λœ μ½”λ“œλ₯Ό μƒλž΅ν•˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? μ΄λ ‡κ²Œ μƒκ²Όλ‚˜μš”?

<BottomNav />

κ·Έλ ‡λ‹€λ©΄ λ¬Έμ œλŠ” someProp μ†Œν’ˆ( Props μ •μ˜μ— 따라 ν•„μš”)을 μ œκ³΅ν•΄μ•Ό ν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

<BottomNav someProp="foo" />

λΆ€λ„λŸ½λ‹€... 였, λΆ€λ„λŸ½λ‹€.
λ‚˜λŠ” TS 톡합에 λ„ˆλ¬΄ μ§‘μ€‘ν•΄μ„œ 주변을 λ‘˜λŸ¬λ³΄κ³  λͺ‡ 걸음 λ’€λ‘œ λ¬ΌλŸ¬λ‚˜λŠ” 것을 μžŠμ—ˆμŠ΅λ‹ˆλ‹€.

κ°μ‚¬ν•©λ‹ˆλ‹€ @pelotom :슀마일:

@otroboe λ¬Έμžμ—΄ 쀑볡도 제거...

type Styles = 'positionFixed';

κ·Έλž˜λ„ μ’€ 더 μ‰¬μ› μœΌλ©΄...

λ„€ 저도 κ·Έλž¬μ–΄μš”, κ°μ‚¬ν•©λ‹ˆλ‹€ :+1:

방금이 λ™μΌν•œ λ¬Έμ œμ— μ§λ©΄ν–ˆμ§€λ§Œ 클래슀 λ˜λŠ” ν•¨μˆ˜μ™€ λ™μΌν•œ νŒŒμΌμ—μ„œ μŠ€νƒ€μΌ 개체λ₯Ό μ΄ˆκΈ°ν™”ν•œ κ²½μš°μ—λ§Œ λ°œμƒν•˜λŠ” κ²ƒμœΌλ‘œ λ‚˜νƒ€λ‚¬μŠ΅λ‹ˆλ‹€. λ˜λŠ” λ‹€λ₯Έ νŒŒμΌμ—μ„œ μŠ€νƒ€μΌμ„ κ°€μ Έμ˜€λŠ” 경우 이 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

μ™œ 이런 일이 λ°œμƒν•˜λŠ”μ§€ λ‹¨μ„œκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

@nishmeht7 자체 ν¬ν•¨λœ μŠ€λ‹ˆνŽ«μ„ κ²Œμ‹œν•  수 μžˆμŠ΅λ‹ˆκΉŒ?

@pelotom 방금 ν•˜λ‚˜ λ§Œλ“œλŠ” μž‘μ—…μ„ ν–ˆμœΌλ©° λ‚΄ μƒŒλ“œλ°•μŠ€ ν™˜κ²½μ—μ„œ 잘 μž‘λ™ν•©λ‹ˆλ‹€. μ €λŠ” ν˜„μž¬ 큰 μ•±μ—μ„œ μž‘μ—… 쀑이며 μ—¬μ „νžˆ mui 버전 1.2.2λ₯Ό μ‚¬μš©ν•˜κ³  있으며 λ‚΄ μƒŒλ“œλ°•μŠ€ ν™˜κ²½ 버전은 μ΅œμ‹  λ²„μ „μž…λ‹ˆλ‹€. λ”°λΌμ„œ 버전을 μ—…κ·Έλ ˆμ΄λ“œν•˜λ©΄ 였λ₯˜λ₯Ό μž¬ν˜„ν•  수 없을 것이라고 μΆ”μΈ‘ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” https://material-ui.com/demos/selects/ 의 κΈ°λ³Έ μ–‘μ‹μ˜ 예λ₯Ό λ”°λžμ§€λ§Œ theme root κ°€ μ—†λ‹€λŠ” λΆˆλ§Œμ„ λ°›μ•˜μŠ΅λ‹ˆλ‹€(μ΅œμ‹  νƒ€μ΄ν”„μŠ€ν¬λ¦½νŠΈ 및 material- ui), form μš”μ†Œμ— ν΄λž˜μŠ€κ°€ μ μš©λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. μœ„μ˜ λ…Όμ˜λ₯Ό 따라 λ³΄μ•˜μ§€λ§Œ 결정적이지 μ•Šμ€ 것 κ°™μŠ΅λ‹ˆλ‹€. 그리고 μ‹€μ œλ‘œ μƒμ†λœ 클래슀 λͺ©λ‘μ—λŠ” form λŒ€ν•΄ μƒμ„±λœ 클래슀 이름이 λˆ„λ½λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μƒμ„±λœ 클래슀 이름을 μˆ˜λ™μœΌλ‘œ μΆ”κ°€ν•œ 경우(개발 λ„κ΅¬μ—μ„œ console.log(theme) λ₯Ό μ‚¬μš©ν•˜μ—¬ withStylesμ—μ„œ 찾을 수 μžˆμŠ΅λ‹ˆλ‹€. λͺ¨λ“  것이 μ •μƒμ΄λ―€λ‘œ λΆ„λͺ…νžˆ ν΄λž˜μŠ€κ°€ μ˜¬λ°”λ₯΄κ²Œ μƒμ„±λ˜κ³  μžˆμ—ˆμŠ΅λ‹ˆλ‹€. withStyles λ₯Ό 톡해 μ „λ‹¬λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. form μš”μ†Œμž…λ‹ˆλ‹€. κ½€ ν˜Όλž€μŠ€λŸ½μŠ΅λ‹ˆλ‹€.

κ·Έλž˜μ„œ μ €λŠ” 이것이 정렬될 λ•ŒκΉŒμ§€ μ§€κΈˆμ€ μŠ€νƒ€μΌλ‘œ λ˜λŒμ•„κ°”μŠ΅λ‹ˆλ‹€.

<form style = {{display:'flex',flexWrap:'wrap'}} autoComplete="off">

@HenrikBechmann typescript에 λŒ€ν•œ μŠ€νƒ€μΌ λ¬Έμ„œλ₯Ό 따라해 λ³΄μ…¨μŠ΅λ‹ˆκΉŒ? κ³Όκ±°μ—λŠ” μ €μ—κ²Œ κ½€ 도움이 λ˜μ—ˆμŠ΅λ‹ˆλ‹€. https://material-ui.com/guides/typescript/

@lookfirst κ°μ‚¬ν•©λ‹ˆλ‹€! λ‚˜λŠ” κ·Έ λ¬Έμ„œλ₯Όλ³΄κ³  (μ²˜μŒμ€ μ•„λ‹ˆμ§€λ§Œ :-)) μ‚¬μš©ν–ˆμŠ΅λ‹ˆλ‹€.

export default withStyles({
  root: {
    display: 'flex',
    flexWrap: 'wrap',
  },
})(BaseForm)

(ν•¨μˆ˜κ°€ μ•„λ‹Œ 객체 전달)

... νƒ€μ΄ν”„μŠ€ν¬λ¦½νŠΈ 였λ₯˜λ₯Ό β€‹β€‹ν”Όν•˜κ³  μƒμ„±λœ 클래슀의 μ‚½μž…μ„ ν™œμ„±ν™”ν–ˆμŠ΅λ‹ˆλ‹€.

λ‹€λ₯Έ 팁이 더 λ³΅μž‘ν•œ ꡬ성에 도움이 되기λ₯Ό λ°”λžλ‹ˆλ‹€.

λ˜ν•œ styles ν•¨μˆ˜μ˜ 더 λ³΅μž‘ν•œ ꡬ쑰가 μž‘λ™ν•¨μ„ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€(μƒμ„±λœ className 을 form μ£Όμž…).

import React from 'react'

import { withStyles, createStyles, Theme } from '@material-ui/core/styles'

/*
    patterned after first demo https://material-ui.com/demos/selects/ for 3.03
    use Typsecript fixes from here: https://material-ui.com/guides/typescript/
*/

const styles = (theme:Theme) => createStyles({
  root: {
    display: 'flex',
    flexWrap: 'wrap',
  },
})

class BaseForm extends React.Component<any,any> {

    render() {
        const { classes } = this.props

        return (
            <form className = {classes.root} autoComplete="off">
                {this.props.children}
            </form>
        )
    }
}

export default withStyles(styles)(BaseForm)

νŽΈμ§‘: @eps1lon 은 WithStyles μ‚¬μš© μ‹œ 이것이 λΆˆν•„μš”ν•˜λ‹€κ³  μ§€μ ν–ˆμŠ΅λ‹ˆλ‹€!

λ‚΄κ°€ μ‚¬μš©ν•˜λŠ” 일뢀 성곡 ν–ˆμ–΄ ReturnType<T> μƒμ„±ν•˜κΈ° ClassKey λ‚˜λ₯Ό μœ„ν•΄λ₯Ό :

import * as React from 'react';

import withStyles, {
  StyledComponentProps, 
  StyleRulesCallback,
} from '@material-ui/core/styles/withStyles';

import { Theme } from '@material-ui/core/styles/createMuiTheme';

const overrideStyles = (theme: Theme) => ({
  root: {
    display: 'flex',
    flexDirection: 'column',
  },
});

type Props = StyledComponentProps<keyof ReturnType<typeof overrideStyles>>;

class MyComponent extends React.PureComponent<Props> {
  render() {
    return <div className={this.props.classes.root}></div>;
  }
}

export default withStyles(overrideStyles as StyleRulesCallback, {withTheme: true})(MyComponent);

keyof ReturnType<typeof styleOverrides> λ₯Ό StyledComponentProps ClassKey ν•˜λ©΄ overrideStyles λ°˜ν™˜λœ κ°œμ²΄μ—μ„œ ν‚€λ₯Ό κ°€μ Έμ˜€κ³  ν•΄λ‹Ή ν‚€ λͺ©λ‘μ„ μˆ˜λ™μœΌλ‘œ μœ μ§€ν•˜μ§€ μ•Šμ•„λ„ λ©λ‹ˆλ‹€. . λ‚΄κ°€ μ•Œμ•„μ°¨λ¦° μœ μΌν•œ 결함은 withStyles ν˜ΈμΆœμ—μ„œ overrideStyles as StyleRulesCallback λ₯Ό μΊμŠ€νŒ…ν•˜μ§€ μ•ŠμœΌλ©΄ μœ ν˜• 였λ₯˜μž…λ‹ˆλ‹€. 이유λ₯Ό 100% ν™•μ‹ ν•  수 μ—†μŠ΅λ‹ˆλ‹€. μ–΄λ–€ μ΄μœ μ—μ„œμΈμ§€ overrideStyles κ°€ 무엇인지 μ΄ν•΄ν•˜μ§€ λͺ»ν•˜λŠ” withStyles 것 κ°™μŠ΅λ‹ˆλ‹€.

λ‹€μ†Œ λ³΅μž‘ν•œ μœ ν˜•μ„ λͺ…ν™•νžˆ ν•˜κΈ° μœ„ν•΄ typeof styleOverrides λŠ” μŠ€νƒ€μΌ 개체λ₯Ό λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ‘œ ν•΄μ„λ©λ‹ˆλ‹€. ReturnType<T> λŠ” μŠ€νƒ€μΌ 개체 자체λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€. keyof λŠ” μŠ€νƒ€μΌ κ°œμ²΄μ—μ„œ ν‚€λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€.

@chrislambe typescript κ°€μ΄λ“œλ₯Ό 확인해야 ν•©λ‹ˆλ‹€. ReturnType 등을 μ‚¬μš©ν•  ν•„μš”λŠ” μ—†μŠ΅λ‹ˆλ‹€. createStyles 및 WithStyles λŠ” λ„μš°λ―Έλ‘œ μΆ©λΆ„ν•΄μ•Ό ν•©λ‹ˆλ‹€.

@eps1lon 였, 정말

fwiw λ‚˜λŠ” createStyles/withStyles μŒμ„ μ‚¬μš©ν•¨μ— 따라 점점 더 μ’‹μ•„ν•©λ‹ˆλ‹€. κΉ”λ”ν•œ μ½”λ“œλ₯Ό ν™λ³΄ν•˜κ³  λͺ¨λ“  μŠ€νƒ€μΌλ§/νƒ€μž…μŠ€ν¬λ¦½νŠΈ 문제λ₯Ό μ²˜λ¦¬ν•˜λ©° 둜컬 쑰건뢀 CSSλ₯Ό μ›ν•˜λ©΄ ν΄λž˜μŠ€λ³΄λ‹€ μš°μ„ ν•˜λŠ” 둜컬 μŠ€νƒ€μΌ 속성을 μƒμ„±ν•©λ‹ˆλ‹€.

멋진!!

@material-ui/ core @ Test does not have required attribute classes λ₯Ό μ–»μŠ΅λ‹ˆλ‹€.

import React from 'react'
import { Theme, WithStyles, withStyles, createStyles } from '@material-ui/core/styles'

const styles = (theme: Theme) => createStyles({
  root: {
    color: theme.palette.action.active
  },
})

interface Props extends WithStyles<typeof styles> {
  asd: boolean
}

class TestComponent extends React.Component<Props> {

  render() {
    const { classes } = this.props

    return (
      <div className={classes.root}>
      </div>
    )
  }
}

const Test = withStyles(styles)(TestComponent)

const a = () => <Test asd={true}/>

@aaronlifton2
WithStylesλ₯Ό μ‚¬μš©ν•˜μ—¬ μ†Œν’ˆ κ°•ν™”λ₯Ό λ”°λ₯΄μ‹­μ‹œμ˜€.

@valoricDe 유슀 λ¬Έμ œλŠ” ν•΄κ²°ν•˜μ…¨λ‚˜μš”?

@TrejGun 방금 ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€. κΈ°λŠ₯ ꡬ성 μš”μ†Œμ™€ @material-ui/ core @

λ‚œ 정말 이해가 μ•ˆ λΌμš”.
λ‹€μŒ λ¬Έμ„œ: https://material-ui.com/guides/typescript/#augmenting -your-props-using-withstyles
이 μ›λž˜ 문제의 문제λ₯Ό μΌμœΌν‚€λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. μ–΄λ””μ—μ„œ ꡬ성 μš”μ†Œλ₯Ό μ‚¬μš©ν•  λ•Œ typescriptλŠ” 클래슀 속성이 withStyles에 μ˜ν•΄ _μ£Όμž…_될 κ²ƒμ΄λΌλŠ” 것을 깨닫지 μ•Šκ³  μ†Œν’ˆμœΌλ‘œ _전달_ν•˜λ„λ‘ μš”κ΅¬ν•©λ‹ˆλ‹€.

λ‚˜λŠ” μ§€κΈˆ λͺ‡ μ‹œκ°„ λ™μ•ˆ μ΄λŸ¬ν•œ 문제λ₯Ό μ½μ—ˆμ§€λ§Œ 정말 이해가 λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. 이 μ‹œμ μ—μ„œ μ–΄λ–€ 도움이라도 λ„ˆλ¬΄ 쒋을 κ²ƒμž…λ‹ˆλ‹€.

μœ„μ˜ μ œμ•ˆμ— λŒ€ν•΄

@cfilipov 당신이 μ‚¬μš©ν•˜λŠ” ꡬ성 μš”μ†Œ 클래슀λ₯Όν•œλ‹€ μž₯식 ν•  λ•Œ StyledComponentProps λŒ€μ‹  WithStyles , 그리고 μ—„κ²©ν•œ νƒ€μž… 검사 λͺ¨λ“œμ—μ„œ 당신은 null이 μ•„λ‹Œ μ£Όμž₯ μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•  경우 ! μΆ”μΆœλ¬Όμ—μ„ classes 의 ν•„λ“œ 이것은 withStyles λ₯Ό 클래슀 λ°μ½”λ ˆμ΄ν„°λ‘œ μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•˜λŠ” μ ˆμΆ©μ•ˆμž…λ‹ˆλ‹€. TypeScript의 클래슀 λ°μ½”λ ˆμ΄ν„°λŠ” λ°˜ν™˜ μœ ν˜•μ΄ 인수 μœ ν˜•κ³Ό μΌμΉ˜ν•΄μ•Ό ν•œλ‹€λŠ” μ œν•œ 이 μžˆμŠ΅λ‹ˆλ‹€. 이 μ œν•œ 사항이 μžˆλŠ” 경우 ν•œ 가지 μ˜΅μ…˜λ§Œ κ°€λŠ₯ν•©λ‹ˆλ‹€.

  1. withStyles λ₯Ό 클래슀 λ°μ½”λ ˆμ΄ν„°λ‘œ μ‚¬μš©ν•˜λŠ” 것을 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
  2. μž₯μ‹λœ ꡬ성 μš”μ†Œ μœ ν˜•μ˜ μš”μ†Œλ₯Ό ꡬ성할 λ•Œ 더미 classes μ†Œν’ˆμ„ μ „λ‹¬ν•˜λ„λ‘ μš”κ΅¬ν•©λ‹ˆλ‹€(이전 μ ˆμΆ©μ•ˆμ€ 틀림없이 더 μ§œμ¦λ‚¬μŒ )
  3. classes κ°€ ꡬ성 μš”μ†Œ λ‚΄μ—μ„œ nullable둜 κ°„μ£Όλ˜λ„λ‘ μš”κ΅¬ν•˜μ—¬ ν•΄λ‹Ή ν•„λ“œμ— μ•‘μ„ΈμŠ€ν•  λ•Œ ! μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€( ν˜„μž¬ νƒ€ν˜‘ ).

ꡬ성 μš”μ†Œμ— μƒνƒœκ°€ μ—†λŠ” 경우 μƒνƒœ λΉ„μ €μž₯ κΈ°λŠ₯ ꡬ성 μš”μ†Œλ₯Ό μ‚¬μš©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€. 그러면 μœ ν˜• 주석이 더 적게 ν•„μš”ν•˜κ³  μœ ν˜•μ΄ 더 μ•ˆμ „ν•©λ‹ˆλ‹€.

export default withStyles(styles)<Props>(({ classes, message }) => (
  <div className={classes.main}>
    <div className={classes.foo}>
      Hello World! {message}
    </div>
  </div>
));

StyledComponentProps μ‚¬μš© 방법을 μ–΄λ–»κ²Œ μ•Œ 수 μžˆμŠ΅λ‹ˆκΉŒ? μŠ€νƒ€μΌ κ°œμ²΄μ— μ •μ˜λœ ν‚€μ˜ λ¬Έμžμ—΄μ„ μ „λ‹¬ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ λ¬Έμ„œλŠ” λ‹¨μˆœνžˆ μž‘λ™ν•˜μ§€ μ•ŠλŠ” 일을 ν•˜λΌκ³  μš°λ¦¬μ—κ²Œ λ§ν•©λ‹ˆκΉŒ? λ‚΄κ°€ 무엇을 λ†“μΉ˜κ³  μžˆμŠ΅λ‹ˆκΉŒ? https://material-ui.com/guides/typescript/#augmenting -your-props-using-withstyles...λ₯Ό μ‚¬μš©ν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

이게 κ°€λŠ₯ν•΄?

@valoricDe , ν•΄λ‹Ή λ¬Έμ œκ°€ μ—†λŠ” κΈ°λŠ₯ ꡬ성 μš”μ†Œλ₯Ό μ–΄λ–»κ²Œ μˆ˜ν–‰ν–ˆμŠ΅λ‹ˆκΉŒ?

@TrejGun 방금 ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€. κΈ°λŠ₯ ꡬ성 μš”μ†Œμ™€ @material-ui/ core @

λ‚˜λŠ” λ‹€μŒκ³Ό 같은 것을 μ‹œλ„ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

```import React, { ChangeEvent, Component, Dispatch } from "react";
"prop-types"μ—μ„œ PropType을 κ°€μ Έμ˜΅λ‹ˆλ‹€.
"react-redux"μ—μ„œ { μ—°κ²° } κ°€μ Έμ˜€κΈ°;
"@material-ui/core"μ—μ„œ { κ·Έλ¦¬λ“œ, FormControlLabel, ν…Œλ§ˆ, createStyles, withStyles, Radio, WithStyles } κ°€μ Έμ˜€κΈ°;
"./Amount"μ—μ„œ κΈˆμ•‘ κ°€μ Έμ˜€κΈ°;
"../../store/actions/selectPaymentAmountActions"μ—μ„œ { onPastDueFormFieldChange } κ°€μ Έμ˜€κΈ°;

const μŠ€νƒ€μΌ = (ν…Œλ§ˆ: ν…Œλ§ˆ) =>
createStyles({
μ–‘: {
alignSelf: "쀑앙",
},
});

μΈν„°νŽ˜μ΄μŠ€ OwnPropsλŠ” WithStylesλ₯Ό ν™•μž₯ν•©λ‹ˆλ‹€.{}

μΈν„°νŽ˜μ΄μŠ€ StateProps {
κΈ°ν•œμ΄ μ§€λ‚¬μŠ΅λ‹ˆκΉŒ?: 번호;
pastDueOrTotalOrOther: λ¬Έμžμ—΄;
}

μΈν„°νŽ˜μ΄μŠ€ DispatchProps {
onPastDueFormFieldChange: OnPastDueFormFieldChange;
}

μœ ν˜• μ†Œν’ˆ = StateProps & DispatchProps & OwnProps;

const PastDueFormField = withStyles(μŠ€νƒ€μΌ)(
({ 클래슀, pastDue, pastDueOrTotalOrOther, onPastDueFormFieldChange }: μ†Œν’ˆ) => (
κ°’ = "μ§€λ‚œ"
checked={κ³Όκ±° 마감일 λ˜λŠ” 전체 λ˜λŠ” 기타 === "κ³Όκ±° 마감일"}
onChange={onPastDueFormFieldChange}
label="κΈ°ν•œ κ²½κ³Ό:"
컨트둀={ }
/>




),
);

const mapStateToProps = (μƒνƒœ: RootState): StateProps => ({
κ³Όκ±° κΈ°ν•œ: state.customerData.balanceDue.pastDue,
pastDueOrTotalOrOther: state.customerPaymentsForm.pastDueTotalOrOther,
});

κΈ°λ³Έ μ—°κ²° 내보내기(
mapStateToProps,
{ onPastDueFormFieldChange },
)(κ³Όκ±° 마감 양식 ν•„λ“œ);

When I use this component I have this error:
```import PastDueFormField
Property 'classes' is missing in type '{}' but required in type 'Readonly<PropsWithChildren<Pick<Pick<Props, "pastDue" | "pastDueOrTotalOrOther" | "onPastDueFormFieldChange"> & StyledComponentProps<"amount">, "classes" | "innerRef"> & OwnProps>>'

@yehudamakarov λ¨Όμ € react-redux 없이 μ½”λ“œλ₯Ό μž‘μ„±ν•˜κ³  λͺ¨λ“  것이 μ˜ˆμƒλŒ€λ‘œ μž‘λ™ν•˜λ©΄ connect μΆ”κ°€ν•˜μ‹­μ‹œμ˜€. prop이 μ£Όμž…λ  λ•Œ 쒋은 κ°œμš”λ₯Ό μ–»λŠ” 것은 믿을 수 없을 μ •λ„λ‘œ μ–΄λ ΅μŠ΅λ‹ˆλ‹€.

μ΄λŸ¬ν•œ λ¬Έμ œκ°€ λ°œμƒν•˜λ©΄

  1. λ‚΄ ꡬ성 μš”μ†Œ μ†Œν’ˆμ„ λ¨Όμ € μž…λ ₯
  2. λͺ¨λ“  것이 μ˜ˆμƒλŒ€λ‘œ ν•„μš”ν•œμ§€ ν™•μΈν•˜μ‹­μ‹œμ˜€. 즉 missing props
  3. μž„μ‹œ 적용
  4. typescriptκ°€ μ£Όμž…λœ λͺ¨λ“  μ†Œν’ˆμ„ μΈμ‹ν•˜λŠ”μ§€ 확인
  5. λͺ¨λ“  혹이 적용될 λ•ŒκΉŒμ§€ 3을 λ°˜λ³΅ν•©λ‹ˆλ‹€.

더 κΉ¨λ—ν•œ μ½”λ“œλ₯Ό μ΄‰μ§„ν•©λ‹ˆλ‹€. 특히 μž‘μ—… μˆœμ„œμ™€ κ΄€λ ¨ν•˜μ—¬. ν˜„μž¬ withStyles 와 connect λ₯Ό κ΅¬λΆ„ν•˜μ§€ μ•Šκ³  ν˜Όν•©ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€.

μ„Έλ°”μŠ€μ°¬ 정말 κ°μ‚¬ν•©λ‹ˆλ‹€.
연결에 일반 인수λ₯Ό μ „λ‹¬ν•˜μ§€ μ•ŠμŒμœΌλ‘œμ¨ 문제λ₯Ό ν•΄κ²°ν–ˆμŠ΅λ‹ˆλ‹€. <StateProps ...> 덩어리λ₯Ό κΊΌλƒˆμŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” κ·Έ 일반 μΈμˆ˜κ°€ WithStyles<>λ₯Ό ν™•μž₯ν•˜λŠ” λ‚΄ OwnProps μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ—‰λ§μœΌλ‘œ λ§Œλ“€κ³  μžˆλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

μ–΄μ¨Œλ“  ꡬ성 μš”μ†Œμ— λͺ¨λ“  것을 μ „λ‹¬ν•˜κΈ° λ•Œλ¬Έμ— λ‚΄ Propsμ—μ„œ 얻은 μœ ν˜• κ²€μ‚¬λ‘œ μΆ©λΆ„ν•©λ‹ˆλ‹€. id에 connect<> μ œλ„€λ¦­μ΄ ν•„μš”ν•œ 이유λ₯Ό 잘 λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€.

감사 ν•΄μš”!

타이핑을 μ˜¬λ°”λ₯΄κ²Œ ν–ˆλ‹€λ©΄ 클래슀λ₯Ό "μž₯식"ν•˜λŠ” λͺ¨μŠ΅μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

type NonStyleProps = {
  text: string
};

const styles = {
  root: {
    backgroundColor: 'red'
  }
};

const DecoratedComponent = withStyles(styles)(
  class extends React.Component<NonStyleProps & WithStyles<'root'>> {
    render() {
      return (
        <div className={this.props.classes.root}>
          {this.props.text}
        </div>
      );
    }
  }
);

그리고 내뢀에 ν…Œλ§ˆ(useTheme())λ₯Ό μΆ”κ°€ν•˜λŠ” 방법은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

const decorate = withStyles((theme: Theme) => ({
  root: {
    display: "flex"
  },
  appBar: {
    zIndex: theme.zIndex.drawer + 1,
    transition: theme.transitions.create(["width", "margin"], {
      easing: theme.transitions.easing.sharp,
      duration: theme.transitions.duration.leavingScreen
    })
  }
})
이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰