react-async-script icon indicating copy to clipboard operation
react-async-script copied to clipboard

Typescript definition?

Open darewreck54 opened this issue 6 years ago • 5 comments

is there any typescript definition for this?

Thanks, Derek

darewreck54 avatar Sep 12 '18 16:09 darewreck54

Nope, but could add them to the DefinitelyTyped reop? https://github.com/DefinitelyTyped/DefinitelyTyped

hartzis avatar Sep 12 '18 17:09 hartzis

@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
	}

}

karlvr avatar Dec 19 '20 03:12 karlvr

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 avatar Dec 19 '20 03:12 karlvr

@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

dasilvaluis avatar Oct 16 '23 10:10 dasilvaluis

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

dasilvaluis avatar Oct 16 '23 12:10 dasilvaluis