react-async-script
react-async-script copied to clipboard
Typescript definition?
is there any typescript definition for this?
Thanks, Derek
Nope, but could add them to the DefinitelyTyped
reop? https://github.com/DefinitelyTyped/DefinitelyTyped
@dozoisch it would be wonderful to having types in the project, if you're interested, this is the file I've created that seems to work:
index.d.ts
declare module 'react-async-script' {
export default function makeAsyncScriptLoader(getScriptUrl: string, options?: MakeAsyncScriptLoaderOptions): <P>(component: React.ComponentType<P>) => React.ComponentType<P>
export interface MakeAsyncScriptLoaderOptions {
options?: Record<string, string>
callbackName?: string
globalName?: string
removeOnUnmount?: boolean
scriptId?: string
}
}
A little enhancement to support the additional props the HOC adds (I didn't do a great job on ref
):
declare module 'react-async-script' {
export default function makeAsyncScriptLoader(getScriptUrl: string, options?: MakeAsyncScriptLoaderOptions): <P>(component: React.ComponentType<P>) => React.ComponentType<P & MakeAsyncScriptLoaderProps>
export interface MakeAsyncScriptLoaderOptions {
options?: Record<string, string>
callbackName?: string
globalName?: string
removeOnUnmount?: boolean
scriptId?: string
}
interface MakeAsyncScriptLoaderProps {
asyncScriptOnLoad?: () => void
ref?: React.RefObject<any>
}
}
@karlvr @dozoisch, wouldn't it more correct to have the entry argument on the asyncScriptOnLoad
, according with src/async-script-loader.js#L127?
asyncScriptOnLoad?: (entry: { loaded: boolean, errored?: boolean }) => void
And inside of MakeAsyncScriptLoaderOptions
, instead of options it is instead attributes
- src/async-script-loader.js#L114.
So have it modified to this:
declare module 'react-async-script' {
export default function makeAsyncScriptLoader(getScriptUrl: string, options?: MakeAsyncScriptLoaderOptions): <P>(component: React.ComponentType<P>) => React.ComponentType<P & MakeAsyncScriptLoaderProps>
export interface MakeAsyncScriptLoaderOptions {
attributes?: Record<string, string>
callbackName?: string
globalName?: string
removeOnUnmount?: boolean
scriptId?: string
}
interface MakeAsyncScriptLoaderProps {
asyncScriptOnLoad?: (entry: { loaded: boolean, errored?: boolean }) => void
ref?: React.RefObject<any>
}
}
Note: omitting the observables as they seem to be an implementation detail that could easily change and not really necessary for the client