Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 02:08
    aminpaks synchronize #42141
  • 02:06
    aminpaks synchronize #42141
  • 02:03
    typescript-bot labeled #42451
  • 02:03
    typescript-bot labeled #42451
  • 02:03
    typescript-bot assigned #42451
  • 02:03
    rbuckton review_requested #42451
  • 02:03
    rbuckton opened #42451
  • 01:21
    andrewbranch edited #42448
  • 01:19
    typescript-bot labeled #40397
  • 01:19
    typescript-bot labeled #42450
  • 01:19
    typescript-bot labeled #42450
  • 01:19
    typescript-bot assigned #42450
  • 01:19
    andrewbranch opened #42450
  • 00:05
    RyanCavanaugh labeled #2521
  • 00:05
    RyanCavanaugh unlabeled #2521
  • 00:05
    RyanCavanaugh unlabeled #2521
  • 00:05
    RyanCavanaugh reopened #2521
  • Jan 21 23:34
    weswigham synchronize #42449
  • Jan 21 23:34
    typescript-bot labeled #42385
  • Jan 21 23:34
    typescript-bot labeled #42449
Shanon Jackson
@ShanonJackson
Is there any inbuilt typeguard that proves something of unknown has a symbol on it
webstrand
@webstrand
Not as far as I know. We can't use [key: symbol]: whatever index signatures, so I suspect that it isn't possible.
Shanon Jackson
@ShanonJackson
Yeah that was my suspicion
AnyhowStep
@AnyhowStep
image.png
Cloned the repo from that tutorial and checked if they had type checks outside <script> tags... They do not
What the fuck, svelte?
AnyhowStep
@AnyhowStep
AGH Fucking svelte
type=1 is the same as type="1". What I wanted is type={1} to use the number 1 and not the string "1"
Ben Carp
@carpben
image.png
Hi,
A bit confused about the above error.
in the first appearance of state.financeAppData it has a specific object type. But in the second appearance it could be undefined
image.png
I know that the event handler could be executed at any time, and if it was a mutable object the value can change, but in this case it is a deep readonly object.
I'd expect the function to have a reference, and since the data is immutable, the reference will point to the same value.
Am I missing something?
JomoPipi
@JomoPipi
@carpben, that's weird. I hope someone has an answer for you.
image.png
Also, does anyone know why this error is happening?
connect expects an AudioNode or AudioParam and I'm passing in an object of type AudioNode | AudioParam.
JomoPipi
@JomoPipi
I might have even asked about this in here before and then forgot the answer..
JomoPipi
@JomoPipi

I just ended up adding this third line to d.ts:

connect(destinationNode: AudioNode, output?: number, input?: number): AudioNode;
connect(destinationParam: AudioParam, output?: number): void;
connect(destination: AudioNode | AudioParam, output?: number): void;

Feel free to let me know if I can do better

webstrand
@webstrand
@JomoPipi That's the correct way to solve it. Typescript does not merge overloads, they have to match individually
1 reply
@carpben It would be awesome if typescript could infer that constants, once determined to be defined, cannot change. Unfortunately, right now, typescript does not share type narrowing information between function contexts.
If you can tolerate it, I'd use state.financeAppData!.payoutInfo Or add a debugAssertNonNull(state.financeAppData).payoutInfo
I personally use the latter when I have a build chain sufficient to allow me to remove the debugAssertNonNull in release builds
webstrand
@webstrand
but you'll have to configure that yourself, there's no convenient tool as far as I am aware
am3on
@am2on:matrix.org
[m]
hello! I have a question about typescript generics but unsure exactly how to phrase it, so I'll demonstrate via brief example
The following works without issue
type SomeGeneric<T> = {
    arr: T[];
}

function someFunction(test: SomeGeneric<any>[]) {
    return test.map(t => t.arr);
}

const one: SomeGeneric<number> = { arr: [1] }
const two: SomeGeneric<string> = { arr: ["5"] }
const three: SomeGeneric<{[k: string]: string}> = { arr: [{"6": "7"}]}

someFunction([ one, two, three ])
but is there anyway to accomplish the same thing without having to type the test parameter as <any>, and without hardcoding <number | string | ... etc> either?
webstrand
@webstrand
function someFunction<T extends SomeGeneric<unknown>>(test: T[]) would work
map isn't clever enough to infer the return type, though.
am3on
@am2on:matrix.org
[m]
i.e., preferably something like function someFunction<T>(test: SomeGeneric<T>[]) but that gives me the error
Type 'SomeGeneric<string>' is not assignable to type 'SomeGeneric<number>'.
webstrand
@webstrand
It seems to work fine on the playground
webstrand
@webstrand
@am2on:matrix.org If you really must have that syntax, you can use overloads:
function someFunction<T>(test: SomeGeneric<T>[]): T[][];
function someFunction<T extends SomeGeneric<unknown>>(test: T[]): T["arr"][];
function someFunction<T>(test: SomeGeneric<T>[]): T[][] {
    return test.map(t => t.arr);
}

const x = someFunction([ one, two, three ]);
const y = someFunction<string | number>([one, two]);
both invocations work. But I wouldn't recommend using overloads unless you know you're going to be calling someFunction with an explicit <string | number>
am3on
@am2on:matrix.org
[m]
I see... thanks!
Julian Gonggrijp
@jgonggrijp
@webstrand #42413 got the "design limitation" label, as you already suspected.
JomoPipi
@JomoPipi
Hi everyone, I was wondering why sometimes I see errors in only the terminal, instead of both the terminal and in the IDE
especially right now, since I added 2 lines of code to d.ts
it made some errors go away in VSCode, but the same error still appears in the terminal
image.png
I tried rerunning tsc -w and restarting VSCode
JomoPipi
@JomoPipi
I just ended up moving those lines from d.ts to my own codebase and that ended up working
webstrand
@webstrand
@jgonggrijp yeah, I've submitted a few "bugs". But that's how they mostly turn out. Contravariance/covariance seems to be a running theme.
@JomoPipi The IDE sometimes gets out of sync. You can use the "Restart tsserver" command to try and get it back into sync. That's what usually works for me.
Trev
@trevdev:matrix.org
[m]

This message is replying to a Matrix event but we were unable to find associated bridged Gitter message to put it in the appropriate threaded conversation.

Switch to what, sorry? Gitter?

Trev
@trevdev:matrix.org
[m]

I'm trying to extend a browser built in feature called MutationObserver to have a property called "count".

const observer = new MutationObserver(callback);
observer.count = 0

I can do this willy nilly in the JS above and then access .count while using the callback. I can't figure out how to make TS happy with this. I've written the following:

class ObserveWithCount extends MutationObserver {
  count: number;
  constructor(callback: () => void) {
    super(callback);
    this.count = 0;
  }
}

const observer = new ObserveWithCount(async () => {
  try {
    const { data } = await axios.get('/cart.js');
    shipping.update(data);
    console.log(data)
    observer.disconnect()
  } catch (error) {
    // Handle error(?)
  }
});

TypeScript is happy with an extended class definition, but when I run this compiled code in the browser, I get: Failed to construct 'MutationObserver': Please use the 'new' operator, this DOM object constructor cannot be called as a function. Any ideas?

Trev
@trevdev:matrix.org
[m]

After reading up, I found a solution that works:

declare global {
  interface MutationObserver {
    count: number
  }
}

const observer = new MutationObserver(async (mutation) => {
  try {
    const { data } = await axios.get('/cart.js');
    if (data.item_count !== observer.count) {
      observer.count = data.item_count;
      shipping.update(data);
    }
  } catch (error) {
    // Handle error(?)
  }
});

Re-declaring MutationObserver as an interface on global seems sloppy, but the code runs without an issue.

Alexandra Parker
@xandris

@trevdev:matrix.org how about declaring it like this?

const observer: MutationObserver & { count?: number; } =  ...

Then you don't need the ambient declaration / interface merging and you reflect that count might be undefined.

Trev
@trevdev:matrix.org
[m]
That looks a lot nicer. I'm gapping on the & operator. Can you point me in the right direction in the docs?