Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. import * as React from 'react'
  2. import { Component } from 'react'
  3.  
  4. type PropTypes = {
  5. str: string
  6. }
  7.  
  8. // Класс компонента
  9. class MyComponent extends Component<PropTypes> {
  10. handlers: Handlers
  11.  
  12. constructor(props) {
  13. super(props)
  14. this.handlers = new Handlers(this)
  15. }
  16.  
  17. public render() {
  18. return <div onClick={this.handlers.onClick}/>
  19. }
  20. }
  21.  
  22. // Абстрактный класс для создания классов с обработчиками
  23. abstract class HandlersCreator<C extends Component> {
  24. props: Pick<C, 'props'> // Пытаемся присвоить тип «props»
  25.  
  26. constructor(public component: Readonly<C>) {
  27. // Тут происходит ругань, что нельзя присвоить, ибо типы
  28. // 'C["props"]' и 'Readonly<{ children?: ReactNode }> & Readonly<{}>'
  29. // несовместимы
  30. this.props = component.props
  31. }
  32.  
  33. public onClick() {
  34. // Тут колдунство для клика
  35. }
  36. }
  37.  
  38. // Класс с обработчиками
  39. class Handlers extends HandlersCreator<MyComponent> {
  40. constructor(component: MyComponent) {
  41. super(component)
  42.  
  43. // Тут тоже ошибка
  44. // Property 'str' does not exist on type 'Pick<MyComponent, "props">'
  45. console.log(this.props.str)
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement