by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 18:12
    csigs synchronize #15575
  • 18:12
    csigs synchronize #28460
  • 18:12
    csigs synchronize #31641
  • 18:11

    csigs on hb_998_20200926181025851

    (compare)

  • 18:11

    csigs on hb_998_20200926181025851

    (compare)

  • 18:11

    csigs on master

    LEGO: check in for master to te… LEGO: Merge pull request 40790 … (compare)

  • 18:10
    typescript-bot labeled #40790
  • 18:10
    csigs closed #40790
  • 18:10
    csigs opened #40790
  • 18:00
    typescript-bot closed #40726
  • 12:45
    pathurs edited #40789
  • 12:43
    pathurs edited #40789
  • 12:43
    pathurs edited #40789
  • 12:43
    pathurs edited #40789
  • 12:42
    pathurs opened #40789
  • 11:54
    manuth opened #40788
  • 11:26
    MaxGraey edited #40782
  • 11:25
    MaxGraey edited #40782
  • 11:00
    Jack-Works opened #40787
  • 10:24
    MaxGraey edited #40782
QP
@theqp
oh
Keith Layne
@keithlayne
@webstrand usually when I need to do a type assertion its a downcast. When it's an upcast I usually I can pass type args.
webstrand
@webstrand

I just really don't like that:

class Foo { foo = 1 }
class Bar extends Foo { bar = 1 }
(new Bar) as Foo; // fine
(new Foo) as Bar; // fine

works

If only one of those worked, I'd be happy. I don't care which one
Keith Layne
@keithlayne
I wonder if eslint can find those.
maybe just use your safeCast and ban as.
webstrand
@webstrand
That's what I've more or less done
I do use the unsafe cast foo as unknown as bar
and sometimes foo as never
Keith Layne
@keithlayne
do you ever use instanceof to narrow?
QP
@theqp
banning is a bad idea for two reasons
  1. your workmates done stupid things intentionally like classInstance.notMember = ...
  2. the type definitions of libraries may be inaccurate
webstrand
@webstrand
@keithlayne Rarely, but in my apps, I don't have significant class heirarchies, so I rarely get to use it
Keith Layne
@keithlayne
banning just requires a comment to turn it off for that line, which makes suspicious uses easy to find.
webstrand
@webstrand
@theqp The point behind banning it is, there are safe (or less confusing) alternatives, and you use those instead
Keith Layne
@keithlayne
what do you use for downcasting?
I guess I'm thinking about validation with instanceof, which is not safe by itself. I don't really do classes, but I use some of the JS builtins.
QP
@theqp
safe casting can be useful for casting to type aliases
webstrand
@webstrand
instanceof is generally safe, unless you're fiddling with the prototypes
Otherwise, I fallback on runtime validation, and manually downcast the object
QP
@theqp
i love using io-ts
webstrand
@webstrand
I am not sure that I've ever had to downcast a class instance, though, where instanceof wasn't sufficient.
And I have never downcast an instance that my code didn't first upcast. I can't trust the consumers of my libraries to get that right.
QP
@theqp
is there a popular plugin for typescript which adds a new feature to the language?
e.g. I wish ifs and scopes were expressions
webstrand
@webstrand
There's ttypescript
at the bottom of that page, it lists a whole bunch of transforms
but none that significantly modify syntax
my fav is https://github.com/LeDDGroup/typescript-transform-macros, but I do not use it in production, too unreliable
Mahesh Sundaram
@TC-mSundaram
Is anyone aware of if it's possible to use proposed ES features, like tc39/proposal-export-default-from - export ns from "./file" in Typescript? I know I can use '@babel/plugin-proposal-export-default-from' but that transforms the JS, not TS? If I run tsc on code using that syntax, it won't compile
matrixbot
@matrixbot
Resynth It might be on the dev branch but it's best to ask first
Lucas Åström
@Jinxit
Is there any workaround to accomplish something similar to microsoft/TypeScript#32705 ? Any runtime cost wouldn't really be a problem, I just want a way to get the type of a module from a non-constant string
Andreas Herd
@mendrik
what would be the correct signature of a sequential promise helper function? I tried something like this: export const seq = <R extends unknown[], P extends Promise<R>[]>(...promises: [...P]): Promise<[...R]>, but I doubt this is correct
Keith Layne
@keithlayne
image.png
um...that's not that helpful.
but you could probably use it directly if I understand correctly - your function should have the same signature as Promise.all, right?
Keith Layne
@keithlayne
@mendrik this may be overly complicated, but seems to work.
downside is that it requires TS 4.1.
actually, the return type can be simplified to Promise<MapAwaited<P>>
wimbarelds
@wimbarelds
can I type something as a module from @types/node ?, something like const bla: FS;
Raziel
@rraziel
typeof?
wimbarelds
@wimbarelds
not sure what id typeof for that?
Keith Layne
@keithlayne
if you've imported a module like import foo from 'bar' you can do const myFoo: typeof foo.
if you just wanna use the types you can do declare const myPath: typeof import('path')
if it's a global you should be able to use typeof.
wimbarelds
@wimbarelds
admittedly my actual problem is something else. I have a Vue app which will be running in an electron app
and I'm having trouble getting access to window.require, so I've had to resort to: const fs = eval("global.require('fs')"); const path = eval("global.require('path')");
which works, but obviously won't give me typing for those, and since they'll both be of type any, I can't typeof on them either
but it seems I can declare type fsType = typeof import('fs'); (declare type instead of declare const)
Keith Layne
@keithlayne
I don't think you need declare there.
I would just do:
const fs = eval("global.require('fs')") as typeof import('fs');
const path = eval("global.require('path')") as typeof import('path');