Material-ui: "TS2554 : 1 개의 μΈμˆ˜κ°€ ν•„μš”ν•˜μ§€λ§Œ 0을 μ–»μ—ˆμŠ΅λ‹ˆλ‹€." makeStyles에 μ˜ν•΄ λ°˜ν™˜ 된 후크

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

  • [x] 이것은 v0.x λ¬Έμ œκ°€ μ•„λ‹™λ‹ˆλ‹€.
  • [x]이 μ €μž₯μ†Œμ˜ 문제 λ₯Ό κ²€μƒ‰ν–ˆμœΌλ©° 이것이 쀑볡이 μ•„λ‹ˆλΌκ³  μƒκ°ν•©λ‹ˆλ‹€.

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

TypeScriptλŠ” λ‹€μŒμ„ μˆ˜ν–‰ ν•  λ•Œ 였λ₯˜λ₯Ό ν‘œμ‹œν•˜μ§€ μ•Šμ•„μ•Όν•©λ‹ˆλ‹€.

ꡬ성 μš”μ†Œ μ™ΈλΆ€ :
const useStyles = makeStyles(styles);

ꡬ성 μš”μ†Œ λ‚΄λΆ€ :
const {/* stuff */} = useStyles();

ν˜„μž¬ 행동 😯

useStyles(); ν•¨μˆ˜ ν˜ΈμΆœμ€ 밑쀄이 κ·Έμ–΄μ Έ 있으며 WebStorm은 "TS2554 : 1 개의 인수λ₯Ό μ˜ˆμƒν–ˆμ§€λ§Œ 0 개λ₯Ό μ–»μ—ˆμŠ΅λ‹ˆλ‹€."라고 λ§ν•©λ‹ˆλ‹€. κ·Έ μœ„μ—.

| 기술 | 버전 |
| -------------- | --------- |
| @ material-ui / styles | 3.0.0-alpha.4 |
| λ°˜μ‘ | 16.7.0-alpha.2 |
| TypeScript | 3.1.1 |

bug πŸ› styles typescript

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

@krazyjakee μ‹œλ„ const c = useStyles({});

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

styles μ‹ κ³  및 tsconfig.json 포함 ν•΄μ£Όμ„Έμš”. tsc μ‹€ν–‰ν•  λ•Œ 였λ₯˜κ°€ ν‘œμ‹œλ©λ‹ˆκΉŒ? IDE 톡합은 λ‹€λ₯Έ typescript 버전을 μ‚¬μš©ν•˜λŠ” κ²½ν–₯이 있기 λ•Œλ¬Έμ— 이것을 λ¬»λŠ”λ‹€.

κ΄€λ ¨μ—†λŠ” nitpick : μ‹œκ°„μ΄ 지남에 따라 λ³€κ²½λœ 버전 λ¬Έμžμ—΄ react@next μ„ ν˜Έν•©λ‹ˆλ‹€. [email protected] μ˜λ―Έν–ˆμ„ κ²ƒμž…λ‹ˆλ‹€.

μ €λŠ” tsc 직접 μ‹€ν–‰ν•˜μ§€ μ•Šκ³  IDEλ₯Ό μ‚¬μš©ν•˜μ—¬ TypeScript νŒŒμΌμ„ λ³€κ²½ν•  λ•Œλ§ˆλ‹€ .ts/x μ—μ„œ .js/x 직접 (μ œμžλ¦¬μ—) λ³€ν™˜ν•©λ‹ˆλ‹€.

μ‚¬μš©μ€‘μΈ WebStormκ³Ό ν•¨κ»˜ λ²ˆλ“€λ‘œ μ œκ³΅λ˜λŠ” 버전은 3.1.1 μž…λ‹ˆλ‹€.

λ‚΄κ°€ μ‚¬μš©ν•˜λŠ” React 버전을 λ°˜μ˜ν•˜λ„λ‘ OPλ₯Ό μ—…λ°μ΄νŠΈν–ˆμŠ΅λ‹ˆλ‹€ : 16.7.0-alpha.2

styles λ³€μˆ˜ :

const styles = {
    chart: {
        width: '100%',
        height: 70,
        backgroundColor: '#f9f9f9'
    },
}

( styles 이 ν•¨μˆ˜ ( theme => { /* definitions */ } )둜 μ •μ˜ 될 λ•Œ λ™μΌν•œ λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€.

tsconfig.json :

{
  "compilerOptions": {
    "sourceMap": false,
    "target": "ES2017",
    "module": "ES6",
    "jsx": "react",
    "moduleResolution": "Node",
    "strictPropertyInitialization": true,
    "strictNullChecks": true,
    "noImplicitAny": true
  }
}

( styles 이 ν•¨μˆ˜ ( theme => { /* definitions */ } )둜 μ •μ˜ 될 λ•Œ λ™μΌν•œ λ©”μ‹œμ§€κ°€ ν‘œμ‹œλ©λ‹ˆλ‹€.

그러면 섀정에도 λ¬Έμ œκ°€ μžˆμŠ΅λ‹ˆλ‹€. 콜백 μ‚¬μš©μ„ ν…ŒμŠ€νŠΈν–ˆμœΌλ©° μ œλŒ€λ‘œ μž‘λ™ν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 정적 μŠ€νƒ€μΌ 객체λ₯Ό μ‚¬μš©ν•  λ•Œ μž¬ν˜„ κ°€λŠ₯ν•œ 버그가 μžˆμŠ΅λ‹ˆλ‹€.

λ¬Έμ œλŠ” [email protected] (컴파일러 ꡬ성 증뢄 : true) 및 @material-ui/[email protected] μ‘°ν•©μœΌλ‘œ λ‹€μ‹œ λ°œμƒν•©λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ [email protected] μ—μ„œλŠ” μž‘λ™ν•©λ‹ˆλ‹€.

@TeoTN μ‹ κ³ 

@TeoTN 문제λ₯Ό μΌμœΌν‚€λŠ” μ½”λ“œλ₯Ό 포함 ν•΄ μ£Ό

@ eps1lon 은 버그λ₯Ό μž¬ν˜„ν•˜λŠ” μ €μž₯μ†Œμž…λ‹ˆλ‹€ : TeoTN / mui-ts-bug

λ‚˜λŠ” λ˜ν•œ [email protected] 및 @material-ui/[email protected] 와 ν•¨κ»˜μ΄ ν‹°μΌ“μ˜ 였λ₯˜λ₯Ό λ°œκ²¬ν–ˆμŠ΅λ‹ˆλ‹€.
[email protected] μ—μ„œ μ—…κ·Έλ ˆμ΄λ“œ ν•œ μ΄μœ λŠ” VS Codeκ°€ μžλ™ μ™„μ„± 및 도ꡬ μ„€λͺ…μœΌλ‘œ 인해 놀라 울 μ •λ„λ‘œ 느리기 λ•Œλ¬Έμž…λ‹ˆλ‹€.

[email protected] λ‹€μš΄ κ·Έλ ˆμ΄λ“œ ν•  λ•Œ makeStyles 주변에 μƒˆλ‘œμš΄ 였λ₯˜κ°€μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

      Types of property 'main' are incompatible.
        Type '{ position: string; top: number; left: number; bottom: number; right: number; }' is not assignable to type 'CSSProperties | ((props: {}) => CSSProperties)'.
          Type '{ position: string; top: number; left: number; bottom: number; right: number; }' is not assignable to type 'CSSProperties'.
            Types of property 'position' are incompatible.
              Type 'string' is not assignable to type 'PositionProperty'.  TS2345

     9 | }));
    10 | 
  > 11 | const useStyles = makeStyles((theme: Theme) => ({
       |                              ^
    12 |   main: {
    13 |     position: 'absolute',
    14 |     top: 0,

λ‹€μŒκ³Ό 같이 createStyles 와 ν•¨κ»˜μ΄ 문제λ₯Ό ν•΄κ²°ν•  μˆ˜μžˆμ—ˆμŠ΅λ‹ˆλ‹€.

const useStyles = makeStyles((theme: Theme) =>
  createStyles({
    main: {
      position: 'absolute',
      top: 0,

μ•„λ§ˆ μ–ΈκΈ‰ ν• λ§ŒνΌ κ°€μΉ˜λŠ” λ‹¨μ§€μž…λ‹ˆλ‹€ import { createStyles } from '@material-ui/styles'; λ™μ•ˆ μž‘ν’ˆ createStyles μ—μ„œ @material-ui/core typings이 λ‹€λ₯΄κΈ°ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ—.

createStyles 은 [email protected] 및 [email protected] λͺ¨λ‘μ—μ„œ ν•„μš”ν•˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

이 λ¬Έμ œλŠ” typescript의 릴리슀 ν›„λ³΄μ—μ„œλ§Œ λ°œμƒν•˜λ―€λ‘œμ΄ 문제λ₯Ό λ‹«κ² μŠ΅λ‹ˆλ‹€. λΆˆμ•ˆμ •ν•œ 쒅속성 버전은 지원할 수 μ—†μŠ΅λ‹ˆλ‹€. μ•ˆμ •λœ λ²„μ „μ˜ Typescriptμ—μ„œμ΄ 였λ₯˜κ°€ λ‹€μ‹œ λ‚˜νƒ€λ‚˜λ©΄ μƒˆλ‘œμš΄ 문제λ₯Ό μ œμΆœν•˜μ‹­μ‹œμ˜€.

ν˜„μž¬ μ΅œμ‹  μ•ˆμ • 버전 인 typescript 3.5.1의 경우 μ‹€μ œ 인 것 κ°™μŠ΅λ‹ˆλ‹€ (material-ui 4와 ν•¨κ»˜).

4.0.2 버전과 ts 3.5.1μ—μ„œλŠ” "strictNullChecks": falseλ₯Ό λ‚΄ tsconfig.json에 μΆ”κ°€ν•˜μ—¬μ΄ 였λ₯˜λ₯Ό 트리거 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

4.0.2 버전과 ts 3.5.1μ—μ„œλŠ” "strictNullChecks": falseλ₯Ό λ‚΄ tsconfig.json에 μΆ”κ°€ν•˜μ—¬μ΄ 였λ₯˜λ₯Ό 트리거 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

이것은 우리의 νƒ€μ΄ν•‘μ—μ„œ μ§€μ›λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ :

우리의 μ •μ˜λŠ” λ‹€μŒ tsconfig.json으둜 ν…ŒμŠ€νŠΈλ©λ‹ˆλ‹€. 덜 μ—„κ²©ν•œ tsconfig.json을 μ‚¬μš©ν•˜κ±°λ‚˜ 일뢀 라이브러리λ₯Ό μƒλž΅ν•˜λ©΄ 였λ₯˜κ°€ λ°œμƒν•  수 μžˆμŠ΅λ‹ˆλ‹€.

-https :

types/ κ²Œμ‹œ 된 λͺ¨λ“  νŒ¨ν‚€μ§€λŠ” "strictNullChecks": false 둜 ν…ŒμŠ€νŠΈλ˜μ§€ μ•Šμ•˜μœΌλ©° 사싀상이 ꡬ성을 μ§€μ›ν•˜λŠ” νŒ¨ν‚€μ§€κ°€ μ—†μŒμ„ μ˜λ―Έν•©λ‹ˆλ‹€.

λ†€λžμ–΄μš”. strict : false둜 ν”„λ‘œμ νŠΈλ₯Ό μƒμ†ν–ˆμœΌλ©° 이전에 더 λ§Žμ€ 였λ₯˜λ₯Ό 생성 ν•œ 적이 μ—†μ—ˆμŠ΅λ‹ˆλ‹€.

v3.xκ°€ μž‘λ™ν–ˆκΈ° λ•Œλ¬Έμ— 4.x도 μž‘λ™ν•œλ‹€κ³  κ°€μ •ν–ˆμŠ΅λ‹ˆλ‹€.

μ–΄μ¨Œλ“ ,이 λ©”λͺ¨λŠ”이 문제λ₯Όλ³΄κ³ ν•˜λŠ” λ‹€λ₯Έ μ‚¬λžŒλ“€μ—κ²Œ 도움이 될 수 μžˆμ§€λ§Œ μ—…κ·Έλ ˆμ΄λ“œμ— λŒ€ν•œ μ˜ˆμƒμΉ˜ λͺ»ν•œ λΉ„μš©μž…λ‹ˆλ‹€.

λ‚΄κ°€ κΈ°μ–΅ν•˜λŠ” ν•œ 3.xμ—λŠ” λ‹€λ₯Έ λ¬Έμ œκ°€μžˆμ—ˆμŠ΅λ‹ˆλ‹€. 당신은 그듀을 λ§Œλ‚˜μ§€ μ•Šμ•˜κ±°λ‚˜ 이미 λΆˆκ±΄μ „ ν•œ μœ ν˜•μ„ κ°€μ‘ŒμŠ΅λ‹ˆλ‹€. 이 μš”κ΅¬ 사항은 4.xμ—μ„œ λ„μž…λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” 거기에 μžˆλ‹€κ³  ν™•μ‹ ν•˜κ³  운이 μ’‹μ•˜μŠ΅λ‹ˆλ‹€.

κ·ΈλŸ¬λ‚˜ strict: true 잘 μ»΄νŒŒμΌλ˜λŠ” μƒ˜ν”Œ ν”„λ‘œμ νŠΈλ₯Ό κ°€μ Έ 였면 λ‹¨μˆœνžˆ strict: false 둜 뒀집어이 문제λ₯Ό ν•΄κ²°ν•  κ²ƒμœΌλ‘œ μ˜ˆμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‚˜λŠ” λΆˆκ±΄μ „ ν•œ μœ ν˜•μ΄ κ·Έ 상황과 μ–΄λ–€ 관련이 μžˆλŠ”μ§€ μ•Œμ§€ λͺ»ν•©λ‹ˆλ‹€.

λ‚˜λŠ” λΆˆκ±΄μ „ ν•œ μœ ν˜•μ΄ κ·Έ 상황과 μ–΄λ–€ 관련이 μžˆλŠ”μ§€ μ•Œμ§€ λͺ»ν•©λ‹ˆλ‹€.

strictNullChecks: false κ΅¬λΆ„λ˜λŠ” μœ ν‹Έλ¦¬ν‹° μœ ν˜•κ³Ό 관련이 μžˆμŠ΅λ‹ˆλ‹€. 이 ν”Œλž˜κ·Έκ°€ μ—†μœΌλ©΄ undefined | null 을 any ν• λ‹Ή ν•  수 μžˆμŠ΅λ‹ˆλ‹€. κ·Έλž˜λ„ κ΅¬ν˜„ μ„ΈλΆ€ μ‚¬ν•­μž…λ‹ˆλ‹€.

이 λ¬Έμ œλŠ” λ‹€μ‹œ μ—΄μ–΄μ•Όν•©λ‹ˆλ‹€!

μ§€κΈˆμ€μ΄ 문제λ₯Ό μ–΄λ–»κ²Œ ν•΄κ²°ν•  수 μžˆμŠ΅λ‹ˆκΉŒ? null을 μ „λ‹¬ν•˜λ©΄ makeStylesμ—μ„œ 'μ •μ˜λ˜μ§€ μ•Šμ€ 클래슀λ₯Ό 찾을 수 μ—†μŒ'였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€. λ•Œλ•Œλ‘œ 톡과 ν•  μ†Œν’ˆμ΄ μ—†λŠ”λ° 무엇을 ν†΅κ³Όν•΄μ•Όν•©λ‹ˆκΉŒ?

@krazyjakee μ‹œλ„ const c = useStyles({});

@krazyjakee μ‹œλ„ const c = useStyles({});

κ°μ‚¬ν•©λ‹ˆλ‹€!

typescript 3.7.5μ—μ„œμ΄ 였λ₯˜κ°€ λ°œμƒν•©λ‹ˆλ‹€.
이 const c = useStyles({}); μž‘λ™ν•©λ‹ˆλ‹€.
κ·ΈλŸ¬λ‚˜ 그것은 λ¬Έμ„œμ™€ μΌμΉ˜ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
https://material-ui.com/zh/styles/basics/

FWIW, 같은 것을 λ΄…λ‹ˆλ‹€. 여기에 κ²Œμ‹œ 된 μ œμ•ˆ (https://github.com/mui-org/material-ui/issues/16867)을 strict 및 strictNullChecks 둜 μ‹œλ„ν–ˆμ§€λ§Œ μ—¬μ „νžˆ μ£Όμ‚¬μœ„λŠ” μ—†μŠ΅λ‹ˆλ‹€. μ§€κΈˆ const c = useStyles({}); "방법"μž…λ‹ˆκΉŒ?

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰