-
-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TypeScript support for @analytics/google-tag-manager #99
Comments
I would like to improve TS support for browser & node packages. Running into some issues where the node/browser types are different but TS only allows for one type definition & doesn't follow the module resolution to grab the proper types. From what I can tell, this might require publishing 2 separate packages. 1 for browser and 1 for serverside node... This is a bummer because it will double the amount of maintenance/packages for the project 😅 The solution proposed looks like a lot of work... Will need to think on this one |
As a workaround I added this code in a Types.ts file
|
What can we do to help @DavidWells? It's unclear what the solution is here since it looks like the typescript files in this repo are generated. |
I'm stuck on the issue where types can be different between server and client implementations https://twitter.com/DavidWells/status/1340059740672442368 It seems like this isn't supported in typescript and this is a real bummer |
Just some ideas: the way I've seen other libraries do it is to have a separate folders. If the code is completely different between the 2 this makes a lot of sense - then the programmer just imports the one that they want. e.g. import Analytics from '@analytics/google-analytics/browser';
// or
import Analytics from '@analytics/google-analytics/node'; I've also seen libraries have a single class but they want to restrict some functions*. So you would have a single class, but export it twice with different types so that the programmer can choose what makes sense for them. e.g. /** This is stuff that is shared to both node.js and browser */
type CommonModules = {
GenericFunc: () => void;
}
/** Browser specific definitions */
type BrowserModules = CommonModules & {
BrowserFunc: () => void;
}
/** Server specific definitions */
type ServerModules = CommonModules & {
ServerFunc: () => void;
}
/** Write all your code */
const AllMyCode:BrowserModules&ServerModules = {
GenericFunc: () => {},
BrowserFunc: () => {},
ServerFunc: () => {}
};
/** Restricted set of browser functions */
const BrowserCode = AllMyCode as BrowserModules;
export BrowserCode;
/** Restricted set of server functions */
const ServerCode = AllMyCode as ServerModules;
export ServerCode;
// Inside user code, they import which one they want
import { BrowserCode, ServerCode } from '@analytics/google-analytics'; Dunno if this helps or not |
Since you're not using typescript internally, there's something to be said here; you're NOT using typescript internally so it's ultimately about exporting types so users can use it. You could export a Browser Type, Node Type, and union type and have your functions use the union types. Being less strict is not ideal, but then you're allowing us to cast to the more specific types when we need to, which solves our issue much more than having no type at all. |
bump |
The original question was about Here are the types for declare module '@analytics/google-tag-manager' {
type AnalyticsPlugin = import('analytics').AnalyticsPlugin;
type GoogleTagManagerConfig = {
auth?: string;
containerId: string;
customScriptSrc?: string;
dataLayerName?: string;
debug?: boolean;
execution?: string;
preview?: string;
};
function googleTagManager(config: GoogleTagManagerConfig): AnalyticsPlugin;
export default googleTagManager;
} |
where do you insert that code? or GeoMarkou declare? |
@santyas You simply put it to |
For the GA4, the declared module should be:
This should be added on |
Unfortunately we still don't have this until today :( |
+1 I just ran into the same issue. Adding types for |
I am using @analytics/google-tag-manager.
I saw analytics have TypeScript support while @analytics/google-tag-manager not.
When add
got
It would be great to add TypeScript in future. Thanks! : )
The text was updated successfully, but these errors were encountered: