ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

Timestamp type not working in react native android

Open damian-bisignano opened this issue 4 years ago • 2 comments

When using a timestamp type toTimestamp & fromTimestamp use underscores as part of the number

function toTimestamp(date: Date): Timestamp {
  const seconds = date.getTime() / 1_000;
  const nanos = (date.getTime() % 1_000) * 1_000_000;
  return { seconds, nanos };
}

When it's running like this get an error

"[SyntaxError: No identifiers allowed directly after numeric literal]"

Not sure whether it's the minification or the JavaScript engine that is used in react-native that causes the issue.

updating it manually to

function toTimestamp(date: Date): Timestamp {
  const seconds = date.getTime() / 1000;
  const nanos = (date.getTime() % 1000) * 1000000;
  return { seconds, nanos };
}

removes the issue.

Not sure if this is something that should just be by default or could be a flag that i'm missing causing this.

damian-bisignano avatar Jun 19 '21 02:06 damian-bisignano

@damian-bisignano can you update your tsconfig to have a lower target: https://www.typescriptlang.org/tsconfig#target

I.e. tsc can understand 1_000 and handle de-sugaring that to just 1000 for older JS runtimes.

stephenh avatar Jun 19 '21 13:06 stephenh

@damian-bisignano If you're still having this issue, I added the babel plugin @babel/plugin-proposal-numeric-separator to transpile these to plain old numeric literals. That seems to work for me.

Updating the tsconfig didn't seem to help for react native as it's using babel to compile the ts to js, and tsc only for type checking.

raymondpebble avatar Nov 05 '21 15:11 raymondpebble