by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 01:07
    typescript-bot synchronize #40748
  • 00:12
    csigs synchronize #15575
  • 00:12
    csigs synchronize #28460
  • 00:12
    csigs synchronize #31641
  • 00:12
    ahejlsberg labeled #40313
  • 00:12
    ahejlsberg unlabeled #40313
  • 00:11

    csigs on hb_998_20200925001053685

    (compare)

  • 00:11

    csigs on master

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

  • 00:11
    typescript-bot labeled #40757
  • 00:11
    csigs closed #40757
  • 00:11
    csigs opened #40757
  • 00:11

    csigs on hb_998_20200925001053685

    LEGO: check in for master to te… (compare)

  • Sep 24 23:50
    amcasey closed #40730
  • Sep 24 23:17
    typescript-bot synchronize #40748
  • Sep 24 22:51
    andrewbranch milestoned #40313
  • Sep 24 22:25
    amcasey synchronize #40730
  • Sep 24 22:20
    typescript-bot synchronize #40748
  • Sep 24 22:05
    mjbvz edited #40756
  • Sep 24 22:05
    mjbvz unassigned #40756
  • Sep 24 22:05
    btakita edited #40747
webstrand
@webstrand
@rekotiira This should do what you want:
export type NoInfer<T> = T & {[K in keyof T]: T[K]};
declare function test<T>(wrapped: Wrapped<T>, fn: (arg: NoInfer<T>) => void): void;
A bit more info about various NoInfer implementations: https://github.com/microsoft/TypeScript/issues/14829#issuecomment-520191642
Reko Tiira
@rekotiira

@webstrand that doesn't work. It'll just infer the type from the Wrapped<T>, which means T is inferred as string | undefined, which means the 2nd argument is going to be fn: (arg: NoInfer<string | undefined>) => void.

(arg: string) => void is assignable to (arg: NoInfer<string | undefined>) => void, so you won't get an error when you do test(foo, strFunc)

webstrand
@webstrand
@rekotiira I'm not clear on what's wrong, here's a demo which one one those the wrong?
Reko Tiira
@rekotiira
Hmm that's weird. I put that exact code in my VSCode and I'm not seeing an error.
webstrand
@webstrand
are you using strict?
Reko Tiira
@rekotiira
yep
webstrand
@webstrand
same version of tsc, or a different one?
Reko Tiira
@rekotiira
3.9.7
webstrand
@webstrand
huh
Does it catch the error when using tsc outside of vscode?
Reko Tiira
@rekotiira
let's try
still no error
hmm actually it seems that our tsconfig.json only has alwaysStrict set to true, but not strict
webstrand
@webstrand
ah "alwaysStrict" is just for added "use strict" to every file
Reko Tiira
@rekotiira
yeah seems so
webstrand
@webstrand
strict is the one that provides additional type safety
NoInfer doesn't work without strictNullChecks (which is implied by strict)
Reko Tiira
@rekotiira
seems like enabling produces quite a bit of errors in our project's codebase :D but I think it's probably going to be a good idea to go through these and fix them
webstrand
@webstrand
yeah, I really, really recommend using strict
Reko Tiira
@rekotiira
well strictNullChecks we actually do have set to true explicitly
could it be the strictFunctionTypes that does the magic?
webstrand
@webstrand
oh yeah, that's the one
Reko Tiira
@rekotiira
thanks a ton, this has been bugging me for days :laughing: should've come and ask about it earlier
I guess there's no way force that error without strict mode? This is library code, so would be nice if anyone using the library would get the error.
webstrand
@webstrand
Nope, it's one of the sad parts of Typescript that they've resisted enabling strict by default
And there's no way to force it from a library
Reko Tiira
@rekotiira
Well I'll take what I can get. If at least I can avoid people from making mistakes in our own project, that's better than nothing :)
Thanks again!
webstrand
@webstrand
most projects use strict, only legacy applications tend to have it disabled
Reko Tiira
@rekotiira
well that's good to hear at least
seems like in strict mode even without NoInfer it gives you an error. then the error is just for the wrapped type arg, while with NoInfer you get the error for the func arg, which is nicer
webstrand
@webstrand
Without strict function types, function arguments are considered "bivariant" more here
Reko Tiira
@rekotiira
@webstrand thanks for the information!
webstrand
@webstrand
I have some files *.script.ts in my code base that are executable scripts. Since they run in a different environement from the rest of my code, they can't share the project's tsconfig.json (which explicitly excludes **/*.script.ts).
vscode is reporting errors in those files due to ES2019 features, is there any way I can set another tsconfig file for .script.ts files?
Salathiel Genèse
@SalathielGenese

Hi everyone,

I have a package - @my/package
It comes with some predefined types.

Now, in @my/project, I linked to @my/package...
But I don't seem to be able to pick on those global types.

How should I go about it, please ?

Raziel
@rraziel
You mean you want its types without importing them?
Salathiel Genèse
@SalathielGenese
Yes @rraziel
That's the spirit
Raziel
@rraziel
Hm then it's tsconfig's typeRoots (iirc)?
Or types
Salathiel Genèse
@SalathielGenese
Hmmmm
Will give it a shot
But I tried types yesterday without much success
darsain
@darsain

I'm running into this kind of issue quite often, where TS complains that bar in returned function is possibly null even though it can't be:

declare function maybeGetList(): any[] | null;

function foo(value: any) {
    let bar = maybeGetList();
    if (!bar) bar = [];
    bar.push(value);
    return () => {
        let index = bar.indexOf(5);
        if (index >= 0) bar.splice(index, 1);
    };
}

what is the most polite way to tell TS to shut up?

or do I have to resort to bar!?
webstrand
@webstrand
let bar = maybeGetList() ?? []; would work
but, yeah, sometimes you have to fall back on using ! or put a runtime check in your anonymous function