TypeScript λ²μ :
1.7.5
μνΈ
class Foo extends React.Component<any, any> {
// ... some class definitions
}
Foo.defaultProps = {
bar: 'baz'
}
μμλλ λμ :
μ€λ₯κ° μμ΅λλ€.
μ€μ νλ :
TS2339 : μμ± 'defaultProps'κ° 'typeof Foo'μ νμ μμ΅λλ€.
μλ νμΈμ, ES6 ν΄λμ€ μ ν¨κ» typescriptλ₯Ό μ¬μ©νμ¬ λ°μ ν΄λμ€λ₯Ό λ§λ€κ³ μΆμ΅λλ€. μμ μ€λ₯κ° λ°μνμ΅λλ€.
ν΄λμ€ λ³Έλ¬Έ λμ μμ κ°μ΄ ν΄λμ€ μμ±μ μ€μ ν μ μλ€λ©΄ λ©μ§ κ²μ λλ€. typescriptκ° μ§μνμ§ μλ κ² κ°μ΅λκΉ?
μ€λ₯λ κΈ°λ³Έμ μΌλ‘ Foo
defaultProps
μ μ μμ±μ΄ μλ€λ κ²μ
λλ€. λ°λΌμ μ»΄νμΌλ¬μκ² κ·Έλ¬ν μμ±μ μνλ€κ³ λ§νλ©΄λ©λλ€.
class Foo extends React.Component<any, any> {
static defaultProps: any;
}
Foo.defaultProps = {
bar: 'baz'
}
μΌλ°μ μΌλ‘ μνμ΄ μ΄λ€ μ’
λ₯μ, μλ€λ©΄ P
λμ any
, κ·Έκ²μ κ² class Foo extends React.Component<P, ...
λ° static defaultProps: P;
μ½κ° λ μ μ μ½λμ λν΄ λͺ¨λ λ³ν©μ μ¬μ©ν μλ μμ΅λλ€.
class Foo { ... }
namespace Foo {
export const defaultProps = {
...
}
}
μΉμ ν λ΅λ³ κ°μ¬ν©λλ€! : +1 :
κ°μ₯ μ μ©ν λκΈ
μ€λ₯λ κΈ°λ³Έμ μΌλ‘
Foo
defaultProps
μ μ μμ±μ΄ μλ€λ κ²μ λλ€. λ°λΌμ μ»΄νμΌλ¬μκ² κ·Έλ¬ν μμ±μ μνλ€κ³ λ§νλ©΄λ©λλ€.μΌλ°μ μΌλ‘ μνμ΄ μ΄λ€ μ’ λ₯μ, μλ€λ©΄
P
λμany
, κ·Έκ²μ κ²class Foo extends React.Component<P, ...
λ°static defaultProps: P;