r/typescript • u/4r73m190r0s • Apr 15 '25
TSServer is server of what?
I know it's not an LSP server, so it's server of what actually?
r/typescript • u/4r73m190r0s • Apr 15 '25
I know it's not an LSP server, so it's server of what actually?
r/typescript • u/Theroonco • Apr 15 '25
Link to my Python backend code.
Link to my separate Axios code (which works).
Link to my Vue project with the Axios code built in (which doesn't).
I posted some API questions here a few days ago. Since then I've ironed out the issues (the backend wasn't accepting json request bodies and I was allowing the wrong origins) and now that I'm using axios to send requests I'm getting a bit more informative error messages. However there seems to be an issue passing information from axios to Vue.
Here's the script portion of GoogleMapsView.vue:
<script setup lang="ts">
import type { MarkerOptions, Position } from '@/models/markerOptions'
import { GoogleMap, Marker } from 'vue3-google-map'
import VesselList from '../components/VesselList.vue'
import { VesselApi } from '@/api/VesselApi'
import type { Vessel } from '@/models/Vessel'
import { ref } from 'vue'
const markers = ref<MarkerOptions[]>([])
// for (let i = 0; i < 9; i++) {
// const pos: Position = {
// lat: i * 10,
// lng: i * 10,
// }
// const mark: MarkerOptions = {
// position: pos,
// label: 'T' + String(i),
// title: 'Test ' + String(i),
// }
// markers.push(mark)
// }
const vessels: Vessel[] = await VesselApi.getAll()
vessels.forEach((vessel: Vessel) => {
const pos: Position = {
lat: vessel.latitude,
lng: vessel.longitude,
}
const marker: MarkerOptions = {
position: pos,
label: String(vessel.id),
title: vessel.name,
}
markers.value.push(marker)
})
const center = ref<Position>({ lat: 0, lng: 0 })
if (markers.value.length) {
const first = markers.value[0]
center.value.lat = first.position.lat
center.value.lng = first.position.lng
console.log(`${center.value}, ${first.position}`)
}
</script>I've followed the execution through using web developer tools and all of this works fine. But something breaks as soon as I get to the template part and I'm not sure what. Everything was fine when I was using dummy data (still present as commented out code) so I'm thinking there's an issue with how the template is reading the markers array? The code isn't tripping any errors though, the webpage just hangs.
If anyone can help me figure this out I'd be very grateful. Thank you in advance!
<template>
<GoogleMap
api-key="AIzaSyAe3a0ujO-avuX4yiadKUVIHyKG5YY83tw"
style="width: 100%; height: 500px"
:center="center"
:zoom="15"
>
<Marker v-for="marker in markers" :key="marker.label" :options="marker" />
</GoogleMap>
<!-- <VesselList :markers="markers" /> -->
</template>r/typescript • u/memo_mar • Apr 14 '25
r/typescript • u/sebastianstehle • Apr 14 '25
Hi,
I would like to migrate some part of my code to use auto generated classes from an OpenAPI spec. I don't want to extend too many changes, so I have to a few additional functions to the generated classes.
My idea was to use prototypes for that, my I cannot figure out how to make the typescript compiler happy. I have tried something like that:
import { EventConsumerDto } from './generated';
interface EventConsumerDto extends EventConsumerDto {
canReset(): boolean;
}
EventConsumerDto.prototype.canReset = () => {
return hasAnyLink(this, 'delete');
};r/typescript • u/18nleung • Apr 14 '25
r/typescript • u/kervanaslan • Apr 14 '25
Hi,
I have a custom Zod schema like this:
// knxGroupAddress.tsx
import { z } from "zod";
const groupAddressRegex = /^\d{1,2}\/\d{1,2}\/\d{1,3}$/;
const knxGroupAddress = () =>
z.string().refine((val) => groupAddressRegex.test(val), {
message: "Invalid KNX group address fromat, must be X/Y/Z",
});
export default knxGroupAddress;
// z-extensions.ts
import { z } from "zod";
import knxGroupAddress from "./knxGroupAddress";
import knxConfiguration from "./knxConfiguration";
export const zodExtended = {
...z,
knxGroupAddress,
knxConfiguration,
};
// metaDataTemplate.tsx
const MetadataTemplate = (
name: string,
description: string,
propsSchema: z.ZodTypeAny,
) =>
z
.object({
name: z.string().default(name),
description: z.string().default(description),
props: propsSchema,
})
.default({});
export default MetadataTemplate;
//knx.tsx
export const DelayMetadataSchema = z.object({
general: MetadataTemplate(
"Delay",
"Use this to delay code",
z.object({
delayMs: z.number().default(1000).describe("delayMilliseconds"),
delayTrigger: z.number().default(1000).describe("Delay Trigger"),
groupAddress: z
.knxGroupAddress()
.default("0/0/0")
.describe("Knx Group Address"),
}),
),
});
export const DelayDataSchema = z.object({
color: z.string().default(ColorPaletteEnum.PURPLE),
label: z.string().default("Delay"),
metadata: DelayMetadataSchema.default({}),
});At runtime, I want to check if a given Zod schema (e.g. schema.metadata.groupAddress) was created using knxGroupAddress()
here is the screen shot of the object.

But it's wrapped in ZodEffects, but I would want to check if it is knxGroupAddress
Is there a clean way to tag custom schemas and detect them later, even through refinements or transforms?
Thanks in advance.
r/typescript • u/seniorsassycat • Apr 13 '25
And how does tsc handle .ts files inside node_moodules? Does it find the types correctly? Does it try to type check internals of the files? Is it slower to parse and type check?
r/typescript • u/Theroonco • Apr 13 '25
I've written an API that accesses a database in Python (code here). Now, when I'm running this FastAPI code, I want to call the endpoints with Typescript. The code I'm writing simulates a record of ships stored in Vessel objects (id, name, longitude, latitude). In Python their types are (integer, string, float, float); in Typescript they're (number, string, number, number).
I've checked StackOverflow for similar issues and found these among others, but they haven't helped.
Using the GetVessels endpoint should give me this:
[
{
"name": "jess",
"latitude": 0,
"id": 1,
"longitude": 0
},
{
"name": "a",
"latitude": 4,
"id": 2,
"longitude": 55
}
]
i.e. an array of Vessel objects. I've tried a few different ways of parsing this in Typescript. Here's the most recent:
export async function getVessels(): Promise<Vessel[]> {
const headers: Headers = new Headers();
headers.set('Accept', 'application/json')
const request: RequestInfo = new Request(
'http://127.0.0.1:8000/',
{
method: "GET",
headers: headers
}
);
return fetch(request)
.then(res => res.json())
.then(res => {
return res as Vessel[]
});
}
However the initial request isn't working and I get "an uncaught TypeError TypeError: failed to fetch" message. I've tried handling it but I can't even tell WHAT Typescript is receiving or why even the initial request is failing in versions of this code where I tried to await everything.
The odd thing is I'm getting 200/OK messages on the Python side so the request IS going through, I just have no idea what's happening immediately afterwards. I'm getting the same error for the other CRUD functions so I'm hoping fixing one will teach me how to fix the rest.
These are the Vessel objects in Python followed by in Typescript:
Base = declarative_base()
class VesselDB(Base):
__tablename__ = "vessels"
id = db.Column(db.Integer, primary_key=True, index=True)
name = db.Column(db.String(100), nullable=False)
latitude = db.Column(db.Float, nullable=False)
longitude = db.Column(db.Float, nullable=False)
interface Vessel {
id: number
name: string
latitude: number
longitude: number
}
export type { Vessel }
Thank you all in advance!
UPDATE: I've added CORSMiddleware to the Python code (check the link above). My current typescript code is available here. I'm currently working on getVessels.ts which is called in GoogleMapsView.vue.
r/typescript • u/rjray • Apr 12 '25
(The repo this is all in is here: https://github.com/rjray/smdb)
I have a project that's slowly turning into a monorepo. Right now I have a server sub-directory and a types sub-directory. Soon there will be a client dir as well, that is expected to share type declarations with the server (hence the "types" dir).
I'm trying to use a path alias in the server's tsconfig.json file to simplify references to types:
{
"compilerOptions": {
"composite": true,
"paths": {
"@smdb-types/*": [
"../types/src/*"
]
},
"incremental": true,
"target": "es2016",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"module": "ES2022",
"moduleResolution": "Bundler",
"baseUrl": "src",
"resolveJsonModule": true,
"allowJs": true,
"outDir": "./dist",
"noEmit": true,
"isolatedModules": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitAny": true,
"skipLibCheck": true
}
}This doesn't work for me, though; none of the files that import types are able to resolve paths that start with @smdb-types/.
There is also a tsconfig.json (much simpler) in the types directory:
{
"compilerOptions": {
"composite": true,
"outDir": "./dist",
"rootDir": "./src",
"declaration": true
},
"include": ["src/**/*"]
}What else might I be missing, here? (Repo is linked at the top of the post.)