- Notifications
You must be signed in to change notification settings - Fork 957
Convert to TypeScript #774
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
Conversation
| Point 1: in your nodeResolve({extensions: ['.ts']})
Point 2: not sure how you declared that global. But make sure it's a So; create a |
| hash?: string; | ||
| }; | ||
| | ||
| export type State = object; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would unknown be a better default? object excludes primitives such as numbers, but there's no reason to restrict state to non-primitives?
This is what I used for the default on DefinitelyTyped.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually:
The state object can be anything that can be serialized.
https://developer.mozilla.org/en-US/docs/Web/API/History/pushState#Parameters
So we should exclude functions amongst other things.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for commenting here. I've updated this type to be object | null, but I'm not sure that's exactly what we want. Ideally I'd like this type to not be too restrictive. I just want to require some kind of an object, not a primitive, so object seemed like a good fit. Yes, there will be problems if it is not serializable, but I'm not aware of a nice, simple way to specify that in TS.
| const PopStateEventType = 'popstate'; | ||
| | ||
| const readOnly = __DEV__ ? obj => Object.freeze(obj) : obj => obj; | ||
| const readOnly = __DEV__ ? (obj: any) => Object.freeze(obj) : (obj: any) => obj; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should be generic:
const readOnly = __DEV__ ? <T>(obj: T) => Object.freeze(obj) : <T>(obj: T) => obj;or
const readOnly = <T>(obj: T) => __DEV__ ? Object.freeze(obj) : obj;any completely disables type checking so it should be avoided, e.g.
declare const myObject: any; myObject.foo.bar.baz // no compile time error, but runtime exception!!For this reason, unknown or generics should be used instead (depending on the use case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the first one! 🕺
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const readOnly: typeof Object.freeze = __DEV__ ? Object.freeze : ((obj:any) => obj);I think this one is better. This means:
const readOnly: { <T>(a: T[]): readonly T[]; <T extends Function>(f: T): T; <T>(o: T): Readonly<T>; }There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great suggestion, thank you @OliverJAsh. I'll fix it.
| Do not forget .js. The package has dependencies. |
d9b4806 to 21e6b8c Compare
Not anymore, @TrySound! 😅 |
DO NOT MERGE
This is a first pass at attempting to convert this codebase to TypeScript. There are still a few TS errors:
import './index.ts', but I can't figure out how to get the build (Rollup) to work w/out the extension__DEV__not being defined, but when I added a globaldeclare, the__DEV__blocks appeared in the production build