Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Repo info
Activity
  • 00:00
    typescript-bot closed #49665
  • Jun 26 21:19
    typescript-bot opened #49687
  • Jun 26 20:00
    typescript-bot closed #49655
  • Jun 26 20:00
    typescript-bot closed #49656
  • Jun 26 18:56
    meiamsome opened #49686
  • Jun 26 18:00
    typescript-bot closed #49667
  • Jun 26 16:58
    ahejlsberg demilestoned #49681
  • Jun 26 16:58
    ahejlsberg milestoned #49681
  • Jun 26 16:58
    ahejlsberg milestoned #49681
  • Jun 26 16:58
    ahejlsberg assigned #49681
  • Jun 26 14:24
    zfeed opened #49685
  • Jun 26 14:22
    typescript-bot labeled #49684
  • Jun 26 14:22
    Andarist opened #49684
  • Jun 26 11:02
    mlesin opened #49683
  • Jun 26 09:07
    DetachHead ready_for_review #49682
  • Jun 26 09:07
    DetachHead synchronize #49682
  • Jun 26 06:07

    github-actions[bot] on main

    Update package-lock.json (compare)

  • Jun 26 04:37
    DetachHead edited #49682
  • Jun 26 04:37
    typescript-bot labeled #49682
  • Jun 26 04:36
    DetachHead opened #49682
[pihentagy]
@pihentagy:matrix.org
[m]
Is there a good tutorial for dockerized development of a typescript project?
Matt
@mattfysh

if I have a class with a private member that is not initialized in constructor, and then another method called ensureMember() that sets the value

how can I make it so that other functions that call ensureMember() don't need to do a check for if (!this.member)?

I thought maybe private ensureMember(): this.member is MyType {} but that doesn't work
Matt
@mattfysh
I guess in each function I could just to const localMember = this.ensureMember() and return it from the ensure fn
Sergey Nikitin
@SergNikitin

If ESM is enabled with typescript 4.7 (by setting "type": "module" in package.json and "module": "Node16", "moduleResolution": "node16" in tsconfig.json), is it possible to import a 3rd-party module that

  • has an .mjs implementation and .d.ts declaration file
  • but doesn't have a .d.mts file
    ?

I bumped into an issue with js-base64 which fits this criteria and import attempts error with

Could not find a declaration file for module 'js-base64'. '/myproj/node_modules/js-base64/base64.mjs' implicitly has an 'any' type

But if I symlink or copy .d.ts file to .d.mts file then all is good

1 reply
Bruce Pascoe
@fatcerberus
@mattfysh Yeah there's no way to express that
It's essentially the same limitation as discussed in #9998
TS doesn't look inside function calls to see what they do
MRNafisiA
@MRNafisiA
Hi.
type A = number | string;
type B<T extends A> = T | undefined;

type C<T extends B<A>> = T extends B<infer U> ? U : never;

type D = C<B<number>>; // typescript says D = A, but I expected it to be number. Am I wrong?
3 replies
How can I fix this, to get number type for D ??
kapilpipaliya
@kapilpipaliya
const getFieldConfiguration = (fieldId: string) => {
  if (fieldId === "all") {
    return fieldConfigurations;
  }

  return fieldConfigurations[fieldId];
};
how to properly write return type for this function?
kapilpipaliya
@kapilpipaliya
const fieldConfigs = getFieldConfiguration("all"); gives wrong type
Aquiles Bailo
@Aquiles86047581_twitter

Hello everyone, I need help with this:
Use the rollDice function to roll the dice 10 times. Push the result of each function call into an array, and then print the results in the console.

I'm using the import module, the function I'm importing is:

function flipCoin(min:number, max:number) {
let flipVal = random.int((min = 0), (max = 1))

any idea how I can do this? Please!! Thanks

baxyz
@baxyz:one.ems.host
[m]
const tosses = [...Array(10).keys()] // create an array of 10 items
    .map(() => flipCoin(min, max)); // fill with tosses
console.log(tosses); // [rand1, rand2, ... rand10]
Is this what you are waiting for?
Rory Smith
@ror-y
might anyone know how to achieve this Cypress setup where there's one index.d.ts per entry (directory)? https://stackoverflow.com/questions/72515614/separate-index-d-ts-per-cypress-entry-instead-of-one-huge-index-d-ts
David Klingenberg
@klinki

Hi guys, is it possible to make conditional type for tuple with type inference?

Very simplified example: (I can provide more detailed one too)

type TypedTuple<T extends AllAssignableTypes> = [
    T,
    GetArgForType<T>,
    string
];

// Here it requires to specify type parameter and cannot infer it from 'A' string inside
const test: TypedTuple<'A'> = [
    'A',
    { a: '', something: 1 },
    'test'
];
Also, this brings the limitation if I want to use it for arrays.
// Here I shouldn't use any, but I need array members to be of various TypedTuple types
const tuplesArray: Array<TypedTuple<any>> = [
    [ '', { a: '', something: 20 }, '' ]
];
baxyz
@baxyz:one.ems.host
[m]
Your example works well:
type AllAssignableTypes = "B" | 5 | boolean | null;

type GetArgForType<T extends AllAssignableTypes> = { a: T, something: number };

type TypedTuple<T extends AllAssignableTypes> = [
    T,
    GetArgForType<T>,
    string
];

const test: TypedTuple<"B"> = [
    "B",
    { a: "B", something: 1 },
    'test'
];

const tuplesArray: Array<TypedTuple<5|"B">> = [
    [ 5, { a: 5, something: 20 }, "test" ],
    [ "B", { a: "B", something: 20 }, "test" ]
];
4 replies
sam-s4s
@sam-s4s
Hello! Quick question...
If I have a class "Blah", and inside that class I want to use the value "this", why does it have the type "this", and not resolve to the type "Blah"?
(I have to cast <Blah>this to get what I want)
Bruce Pascoe
@fatcerberus
Because this might be a subclass
it's generic
So you can think of it as having type this: this extends Blah
Instant Kafka
@InstantKafka_twitter
Every time I pop in here to ask a question, I set up a playground to describe my problem and realize that I had made a typo or some other dumb mistake and answer my own question. It always helps to just isolate the problem or code in question.
Bruce Pascoe
@fatcerberus
that's probably a good thing since this channel is pretty much dead, the community moved to discord
3 replies
sam-s4s
@sam-s4s

that's probably a good thing since this channel is pretty much dead, the community moved to discord

oh I didn't know that! I guess I should hunt that down lol

Matt
@mattfysh
How can i force typescript to use types from definitelytyped, and not the ones included in the package (acorn)?
Aaron Beall
@aaronbeall
I thought there was a way to make this work...
const stringsOrNull = ["foo", null, "bar", null]
const onlyStrings = stringsOrNull.filter(s => typeof s == "string")
// I want `onlyStrings` to be of type `string[]` but it's still `(string | null)[]`
Bruce Pascoe
@fatcerberus
(s): s is string => ...
TS doesn't infer type guards
Aaron Beall
@aaronbeall
Oh I see, I was remembering use with actual type guards
const onlyStrings = stringsOrNull.filter((s): s is string => typeof s == "string")
Bruce Pascoe
@fatcerberus
Yeah, if you already have a function isString or something that's declared as a type guard you can pass that to filter
Aaron Beall
@aaronbeall
:thumbsup:
mark
@gmae199boy
hello. I'm new to typescript. I ask because there is only 1 error in 2 of the same code.
import { Result } from "url-metadata";
import { Mongo } from "../lib/db";

export async function UpsertMetadata(_c: Mongo) {
    const collection = _c.Collections.get("metadatas");
    if (!collection) return;

    const result = await collection.updateOne();

    return result;
}

export async function FindAllMetadata(_c: Mongo) {
    const collection = _c.Collections.get("metadatas");
    if (!collection) return;

    const list = await collection.find().toArray();

    return list;
}
image.png
image.png
mark
@gmae199boy
There is no recursive code here.
mark
@gmae199boy
I think I found it. It seems to be a problem with the mongodb package, not a typescript problem. There is no error in .find, only updateOne gives an error.
Mukul Jain
@mukuljainx
Screenshot 2022-06-13 at 8.24.56 PM.png

Hi everyone, I was watching https://www.youtube.com/watch?v=nNse0r0aRT8 and trying to replicate few things like IndexOf,
getting the following error The type 'readonly [1, 2, 3, 4, 5]' is 'readonly' and cannot be assigned to the mutable type '[]'.

CSB: https://codesandbox.io/s/affectionate-darkness-6q09do?file=/src/index.ts

P.S.: Those es-disabled are probably not useful here as I can't see ESlint in package

1 reply
Shawn Corrigan
@scorgn
Is there a way to type hint something as being a static constant of a specific class? I imagine not but I thought it'd be worth asking in case anyone had an idea
Shawn Corrigan
@scorgn
Nevermind it was simple, just keyof typeof Class :D
Jacques Yakoub
@jy95

Hello,

I wonder what is the proper way to define a string that could grow without any limit with https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-1.html#template-literal-types ?

type D1 = 0|1;
type D2 = D1|2;
type D3 = D2|3;
type D5 = D3|4|5;
type D9 = D5|6|7|8|9;
type D9_bis = 1|2|3|4|5|6|7|8|9;
type D12 = D9|10|11|12;


// Bad because of the circular reference 
// Explanation in https://github.com/microsoft/TypeScript/pull/40580 is unclear IMO
type HoursDeep = `${D9}${D9}` | `${D9_bis}${HoursDeep}`;
// Currently only cover "00" to "9999"
// type Hours = `${D9}${D9}` | `${D9_bis}${D9}${D9}${D9 | ''}`;

// Needed for define something like that
type Minutes = `${D5}${D9}`;
type Seconds = `${D5}${D9}`;
type Duration = `${HoursDeep }:${Minutes}:${Seconds}`;

Thanks in advance

Gerrit Birkeland
@Gerrit0
Id argue the proper way is to not. Don't make the compiler construct massive unions if at all possible. Your compile times will thank you.
Bruce Pascoe
@fatcerberus
If the string can grow without limit then the compiler has to construct a union with an infinite number of possible strings, which is of course impossible
Joe S
@JoeS89189155_twitter
Hello,
Can someone tell me why I can call the function in the example with an immutable? How can I fix this issue?
type DeepImmutableObject<T> = {
    readonly [K in keyof T]: Immutable<T[K]>
};

type Immutable<T> =
    Readonly<DeepImmutableObject<T>>;


interface MyType { 
  isImmutable: boolean;
}

function makeTypescriptUseless(screw: MyType) {
  screw.isImmutable = false;
};

const superSafeObject: Immutable<MyType> = { isImmutable: true };

makeTypescriptUseless(superSafeObject);
Bruce Pascoe
@fatcerberus
because { readonly foo: string } is assignable to { foo: string }. readonly doesn't actually mean immutable, it just means you can't modify the property through the readonly type
zhuijing
@zhuijing
汉兜弹幕版,https://b23.tv/BggRC8S
Bruce Pascoe
@fatcerberus
what