meliorence / react-native-render-html

iOS/Android pure javascript react-native component that renders your HTML into 100% native views
https://meliorence.github.io/react-native-render-html/
BSD 2-Clause "Simplified" License
3.48k stars 589 forks source link

Move config for img and a into named interface #606

Closed TuurDutoit closed 1 year ago

TuurDutoit commented 1 year ago

Checks

Description

We are using this library to render HTML in several places in the DataCamp app. We have a custom renderer for <img> tags, which we need to pass some extra config (props) to. I tried using TypeScript module augmentation to add those props to the RenderersProps['img'] type, as the docs suggest:

declare module 'react-native-render-html' {
  export interface RenderersProps {
    img?: {
      fullImageTappable: boolean
    }
  }
}
export {}

Unfortunately, this didn't work: the new field was not picked up by TS. Apparently, you can add new properties on the RenderersProps type this way (i.e. for new element types), but you can't update the built-in ones, as they are anonymous object types. For example, the following does work as expected:

declare module 'react-native-render-html' {
  export interface RenderersProps {
    div?: {
      someProp: boolean
    }
  }
}
export {}

However, this is not what we needed: we want to add a prop for an existing element. By moving those types to named interfaces, it's now possible to extend them:

declare module 'react-native-render-html' {
  export interface ImgElementConfig {
    fullImageTappable?: boolean
  }
}
export {}
jsamr commented 1 year ago

@TuurDutoit you are correct in that only interfaces can be augmented; appreciate the fix, thank you! Will approve as soon as CI passes.

TuurDutoit commented 1 year ago

@jsamr ah right, didn't notice that! I fixed the issue and yarn test is passing locally for me now.

codecov-commenter commented 1 year ago

Codecov Report

Merging #606 (77be4d8) into master (7c79513) will not change coverage. The diff coverage is n/a.

@@           Coverage Diff           @@
##           master     #606   +/-   ##
=======================================
  Coverage   76.92%   76.92%           
=======================================
  Files          83       83           
  Lines         637      637           
  Branches      147      147           
=======================================
  Hits          490      490           
  Misses          7        7           
  Partials      140      140           

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more