From 9c1f25aa0948ed182cce5ec393f6592d34293bc7 Mon Sep 17 00:00:00 2001 From: Ludwig Stockbauer-Muhr Date: Wed, 2 Oct 2024 19:38:09 +0200 Subject: [PATCH 1/5] feat: add type specific properties to linked classes --- apis/linked/classes.d.ts | 5 ++++- apis/linked/internal.d.ts | 4 +++- test/typescript/apis/project/cds-linked.ts | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/apis/linked/classes.d.ts b/apis/linked/classes.d.ts index e0eaec15..6bbc5234 100644 --- a/apis/linked/classes.d.ts +++ b/apis/linked/classes.d.ts @@ -79,7 +79,10 @@ declare class Int32 extends Integer { } declare class Int64 extends Integer { } declare class Float extends number { } declare class Double extends Float { } -declare class Decimal extends Float { } +declare class Decimal extends Float { + precision?: number + scale?: number +} declare class date extends scalar { } declare class Date extends date { } diff --git a/apis/linked/internal.d.ts b/apis/linked/internal.d.ts index 2fd5546f..ba8872ae 100644 --- a/apis/linked/internal.d.ts +++ b/apis/linked/internal.d.ts @@ -14,5 +14,7 @@ import type { scalar } from './classes' declare class number_ extends scalar { } -declare class string_ extends scalar { } +declare class string_ extends scalar { + length?: number +} declare class boolean_ extends scalar { } diff --git a/test/typescript/apis/project/cds-linked.ts b/test/typescript/apis/project/cds-linked.ts index 8b8afe23..c8fe12ec 100644 --- a/test/typescript/apis/project/cds-linked.ts +++ b/test/typescript/apis/project/cds-linked.ts @@ -3,7 +3,7 @@ import cds from '@sap/cds'; import { csn } from '../../../..'; import { as } from './dummy'; -const { action, aspect, entity, event, mixin, scalar, struct, type } = cds.linked.classes +const { action, aspect, entity, event, mixin, scalar, struct, type, Decimal, String } = cds.linked.classes // is exported from top level as() === as() @@ -83,6 +83,11 @@ new action().kind === 'action' // @ts-expect-error new action().kind === 'action_' +new Decimal().precision +new Decimal().scale + +new String().length + mixin(class {}, class {}) // @ts-expect-error mixin(42) From 4412def773adbb405e93380bb82a5e1789186884 Mon Sep 17 00:00:00 2001 From: Ludwig Stockbauer-Muhr Date: Wed, 2 Oct 2024 19:38:52 +0200 Subject: [PATCH 2/5] feat: add all available cds types to csn type property --- apis/csn.d.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apis/csn.d.ts b/apis/csn.d.ts index 9449bfd0..c8e40c75 100644 --- a/apis/csn.d.ts +++ b/apis/csn.d.ts @@ -66,7 +66,12 @@ export interface context extends any_ { } export interface service extends any_ { } export interface type extends any_ { - type?: FQN + type?: 'cds.Boolean' | + 'cds.UUID' | 'cds.String' | 'cds.LargeString' | 'cds.Binary' | 'cds.LargeBinary' | 'cds.Vector' | + 'cds.Integer' | 'cds.UInt8' | 'cds.Int16' | 'cds.Int32' | 'cds.Int64' | 'cds.Float' | 'cds.Double' | 'cds.Decimal' | + 'cds.Date' | 'cds.Time' | 'cds.DateTime' | 'cds.Timestamp' | + 'cds.Association' | 'cds.Composition' | + FQN & Record // allow any other CDS type as well (e.g. 'User') items?: type } @@ -106,7 +111,6 @@ export type EntityElements = { } export interface Association extends type { - type: 'cds.Association' | 'cds.Composition' target: FQN /** From f6f9ff35d58b01a27404b98a0ed61b25d9758410 Mon Sep 17 00:00:00 2001 From: Ludwig Stockbauer-Muhr Date: Wed, 2 Oct 2024 19:42:30 +0200 Subject: [PATCH 3/5] feat: add general properties to 'type' linked type --- apis/linked/classes.d.ts | 3 +++ test/typescript/apis/project/cds-linked.ts | 3 +++ 2 files changed, 6 insertions(+) diff --git a/apis/linked/classes.d.ts b/apis/linked/classes.d.ts index 6bbc5234..257a22f8 100644 --- a/apis/linked/classes.d.ts +++ b/apis/linked/classes.d.ts @@ -55,6 +55,9 @@ declare class aspect extends type implements With } declare interface type extends Omit { items: type + key?: boolean + notNull?: boolean + virtual?: boolean } declare class type extends any_ { } diff --git a/test/typescript/apis/project/cds-linked.ts b/test/typescript/apis/project/cds-linked.ts index c8fe12ec..f03fd68e 100644 --- a/test/typescript/apis/project/cds-linked.ts +++ b/test/typescript/apis/project/cds-linked.ts @@ -73,6 +73,9 @@ new scalar().kind === 'scalar_' new type().kind === 'type' // @ts-expect-error new type().kind === 'type_' +new type().key +new type().virtual +new type().notNull new event().elements new event().kind === 'event' From e3cefc655e1182014b0f4ce1c1cc137aa6ad4db4 Mon Sep 17 00:00:00 2001 From: Ludwig Stockbauer-Muhr Date: Wed, 2 Oct 2024 20:03:55 +0200 Subject: [PATCH 4/5] refactor: make items property of linked 'type' optional - only arrayed elements or array types have this property --- apis/linked/classes.d.ts | 2 +- test/typescript/apis/project/cds-linked.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/apis/linked/classes.d.ts b/apis/linked/classes.d.ts index 257a22f8..87c47645 100644 --- a/apis/linked/classes.d.ts +++ b/apis/linked/classes.d.ts @@ -54,7 +54,7 @@ declare class aspect extends type implements With elements: Definitions> } declare interface type extends Omit { - items: type + items?: type key?: boolean notNull?: boolean virtual?: boolean diff --git a/test/typescript/apis/project/cds-linked.ts b/test/typescript/apis/project/cds-linked.ts index f03fd68e..c5a1f3dc 100644 --- a/test/typescript/apis/project/cds-linked.ts +++ b/test/typescript/apis/project/cds-linked.ts @@ -47,7 +47,7 @@ new entity().texts?.kind === 'entity' new entity().drafts?.kind === 'entity' new entity().is_entity === true new entity().is_struct === true; -[...new entity().elements].find(x => x.items.kind === 'type') +[...new entity().elements].find(x => x.items && x.items.kind === 'type') new entity().items?.kind new entity().name // @ts-expect-error From a34c4d371a91125800c932ee41fed517bd336d87 Mon Sep 17 00:00:00 2001 From: Ludwig Stockbauer-Muhr Date: Wed, 2 Oct 2024 20:51:10 +0200 Subject: [PATCH 5/5] feat: add type specific properties to csn type elements --- apis/csn.d.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/apis/csn.d.ts b/apis/csn.d.ts index c8e40c75..fc941924 100644 --- a/apis/csn.d.ts +++ b/apis/csn.d.ts @@ -107,6 +107,9 @@ export type EntityElements = { virtual?: boolean, unique?: boolean, notNull?: boolean, + precision?: number, + scale?: number, + length?: number, }, }