-
Notifications
You must be signed in to change notification settings - Fork 12
Strongly type Message and use in main signature #16
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
|
Too many modifications. Its difficulty to understand what do you want from this PR or solve some problems? |
|
Where are the descriptions going in the exported json file? |
|
I have a question for you @seanf , as you seem to have a better understanding of the typescript reflection api. I currently have a small issue with and what does not work: How do you debug this library's code? Do you simply use Thank you in advance! I'm still unsure if it would be a good idea to parse every Anyways let me know what you guys think! |
|
You're right. I'll split it into two PRs. One for the changes to Message,
one for the strict null check refactoring.
…On Jan 17, 2018 00:24, "Qibang" ***@***.***> wrote:
Too many modifications. Its difficulty to understand what do you want from
this PR or solve some problems?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#16 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADTiw-xgB0HlzdHKR8ERA8v5pqOfFLfks5tLLEVgaJpZM4Rf1DR>
.
|
|
I actually put in a TODO about that. The call to defineMessages and
formatMessage are currently only picked up if they are used in a variable
declaration.
@BANG88, is there a reason why defineMessages does that? I'm thinking we
could just pick up any function call to either method. Even without a var
declaration.
Maxime, as a workaround, you could use defineMessages first, then format
one of the messages in the if statement. I'm not sure if babel-react-intl
picks up calls to formatMessage without defineMessages.
…On Jan 17, 2018 05:11, "Maxime Beauchamp" ***@***.***> wrote:
I have a question for you @seanf <https://github.com/seanf> , as you seem
to have a better understanding of the typescript reflection api.
I currently have a small issue with formatMessage, here's the example of
what actually works:
const message = formatMessage({id: "test", defaultMessage: "test message"});
and what does not work:
let message;
if (condition) {
message = formatMessage({id: "test_1", defaultMessage: "test message 1"});
} else {
message = formatMessage({id: "test_2", defaultMessage: "test message 2"});
}
How do you debug this library's code? Do you simply use console.log to
know what is what or do you have another technique?
Thank you in advance!
I'm still unsure if it would be a good idea to parse every formatMessage
function calls because sometimes I use it with non static ids like this;
formatMessage({id: object.id, defaultMessage: object.defaultMessage})
Anyways let me know what you guys think!
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#16 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADTiyr8pLoXY5KGRWpf1e5RbLZqQm1cks5tLPRPgaJpZM4Rf1DR>
.
|
|
@seanf Ah I didn't see the TODO. Yea I'm using |
|
@mbeauchamp7
Mostly https://github.com/dsherret/ts-ast-viewer I also found this article useful: http://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html |
We only pick up function calls which pass in an object declaration, where the keys and values are statically known (identifiers and literals). I think babel-plugin-react-intl goes one better and can pick up things like concatenated string constants&literals: https://github.com/yahoo/babel-plugin-react-intl/blob/6f4a533921c8632f53218c00cc3be3042814eed8/src/index.js#L31-L40 Either way, this would skip dynamic examples like this: @BANG88 @mbeauchamp7
It may not be easy to replicate another one of babel-plugin-react-intl's features - I think it might be able to handle renamed imports, eg So it might be good to list that as a known limitation (along with the concatenated constants thing, which I already ran into). |
|
@seanf @mbeauchamp7 This library may not covered every use case. at first my point is make it work so PR are always welcome. If we want export the message interface I think should be something like this: export interface Message {
id: string
defaultMessage: string
description?: string
} |
|
@seanf @BANG88 To be honest, I'm fine with how the library is actually behaving. Ignoring files that don't import I would like to keep the parsing of |
|
I think that's what I've used. Is it not? (Although I put id last, since
tslint likes keys to be in order when you actually make one of these
objects.)
But as I said, I will separate that stuff from all the null check stuff. It
should be clearer then.
…On 17 January 2018 at 11:21, Qibang ***@***.***> wrote:
@seanf <https://github.com/seanf> @mbeauchamp7
<https://github.com/mbeauchamp7> This library may not covered every use
case. at first my point is make it work so PR are always welcome.
If we want export the message interface I think should be something like
this:
export interface Message {
id: string
defaultMessage: string
description?: string
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#16 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AADTi3X_ziT5xyzEn6CJM7FP86_3udKvks5tLUr7gaJpZM4Rf1DR>
.
|
|
Assuming #17 goes in, I'll rebase this on top, then the diff should just show Message-related changes. |
e462d9d to
b2e7645
Compare
|
I have another commit which cleans up the JSX types (no more |
index.ts
Outdated
| interface Message { | ||
| [key: string]: string; | ||
| // a Message with nullable fields, still under construction | ||
| interface PartialMessage { |
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 could be done via Partial
index.ts
Outdated
| // just a map of string to string | ||
| interface Message { | ||
| [key: string]: string; | ||
| // a Message with nullable fields, still under construction |
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 could be done via Partial<Message>
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.
Yes, I just discovered that, thanks. I need to make things a bit more idiomatic.
index.ts
Outdated
| } | ||
|
|
||
| // are the required keys of a valid Message present? | ||
| function isValidMessage(obj: PartialMessage): obj is Message { |
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.
difficulty to understand why need obj is Message can be inferred by TS
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 think it's because the copying happens dynamically. There's no static way of knowing whether those property assignments will occur.
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.
No. from this method your result must be a boolean type. am I right ?
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 another of those user-defined type guards.
| return jsxMessages.concat(dm).concat(fm); | ||
| } | ||
|
|
||
| function notNull<T>(value: T | null): value is 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.
Does this return a boolean value. if so why need value is 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.
It does return a boolean at runtime, but this bit of typing enables the result of filter to be Message instead of Message | null as far as the type checker is concerned. It's a user-defined type guard: https://www.typescriptlang.org/docs/handbook/advanced-types.html
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.
@seanf Thanks. I didn't use this type guard before. 😄
|
could you rebase it so we can merge this PR. |
When you say rebase, do you mean I should rebase the 7 commits on top of master? They're kind of a mess, so I would advise "squash and merge" instead. But if you want to rebase then merge, GitHub usually lets you do that (hit the green drop-down and select "rebase and merge") - it leaves the original commits as they are in the PR for historical purposes. Or did you want me to squash it into one commit, and force push to this branch before you merge? I'm happy to do that if that's what you mean, but again, GitHub can do that automatically if you "squash and merge" (again it leaves the original commits in the PR). (Whereas if I force push a squashed merge, the history of this code review pretty much goes away.) Either way, let me know what you want. Unless you just want to "squash and merge", in which case go ahead! |
|
No description provided.