Skip to content

fix: optional parameter for object type #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ function typeFromSchema(schema: any, isSubType = false): string {
case 'object':
if (schema.properties) {
const propertyKeyValues = Object.entries(schema.properties).map(([key, value]) => {
return `'${key}': ${typeFromSchema(value, true)}`
const isRequired = schema.required?.includes(key)
const optionalChar = isRequired ? '' : '?'
return `'${key}'${optionalChar}: ${typeFromSchema(value, true)}`
})

types.push(`{ ${propertyKeyValues.join('; ')} }`)
Expand Down
10 changes: 5 additions & 5 deletions test/output/i18n-ally.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export interface ConfigKeyTypeMap {
"i18n-ally.regex.key": (string | undefined),
"i18n-ally.regex.usageMatch": (string[] | undefined),
"i18n-ally.regex.usageMatchAppend": (string[] | undefined),
"i18n-ally.refactor.templates": ({ 'source': ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template': string; 'templates': string[]; 'include': string[]; 'exclude': string[] }[] | undefined),
"i18n-ally.refactor.templates": ({ 'source'?: ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template'?: string; 'templates'?: string[]; 'include'?: string[]; 'exclude'?: string[] }[] | undefined),
"i18n-ally.translate.saveAsCandidates": boolean,
"i18n-ally.translate.fallbackToKey": boolean,
"i18n-ally.translate.engines": ("google" | "google-cn" | "deepl" | "libretranslate" | "baidu" | "openai")[],
Expand Down Expand Up @@ -627,7 +627,7 @@ export interface ConfigShorthandTypeMap {
regexKey: (string | undefined),
regexUsageMatch: (string[] | undefined),
regexUsageMatchAppend: (string[] | undefined),
refactorTemplates: ({ 'source': ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template': string; 'templates': string[]; 'include': string[]; 'exclude': string[] }[] | undefined),
refactorTemplates: ({ 'source'?: ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template'?: string; 'templates'?: string[]; 'include'?: string[]; 'exclude'?: string[] }[] | undefined),
translateSaveAsCandidates: boolean,
translateFallbackToKey: boolean,
translateEngines: ("google" | "google-cn" | "deepl" | "libretranslate" | "baidu" | "openai")[],
Expand Down Expand Up @@ -1753,7 +1753,7 @@ export interface ScopedConfigKeyTypeMap {
"regex.key": (string | undefined),
"regex.usageMatch": (string[] | undefined),
"regex.usageMatchAppend": (string[] | undefined),
"refactor.templates": ({ 'source': ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template': string; 'templates': string[]; 'include': string[]; 'exclude': string[] }[] | undefined),
"refactor.templates": ({ 'source'?: ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template'?: string; 'templates'?: string[]; 'include'?: string[]; 'exclude'?: string[] }[] | undefined),
"translate.saveAsCandidates": boolean,
"translate.fallbackToKey": boolean,
"translate.engines": ("google" | "google-cn" | "deepl" | "libretranslate" | "baidu" | "openai")[],
Expand Down Expand Up @@ -1938,7 +1938,7 @@ export interface NestedConfigs {
"usageMatchAppend": (string[] | undefined),
},
"refactor": {
"templates": ({ 'source': ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template': string; 'templates': string[]; 'include': string[]; 'exclude': string[] }[] | undefined),
"templates": ({ 'source'?: ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template'?: string; 'templates'?: string[]; 'include'?: string[]; 'exclude'?: string[] }[] | undefined),
},
"translate": {
"saveAsCandidates": boolean,
Expand Down Expand Up @@ -2083,7 +2083,7 @@ export interface NestedScopedConfigs {
"usageMatchAppend": (string[] | undefined),
},
"refactor": {
"templates": ({ 'source': ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template': string; 'templates': string[]; 'include': string[]; 'exclude': string[] }[] | undefined),
"templates": ({ 'source'?: ("html-attribute" | "html-inline" | "js-string" | "js-template" | "jsx-text"); 'template'?: string; 'templates'?: string[]; 'include'?: string[]; 'exclude'?: string[] }[] | undefined),
},
"translate": {
"saveAsCandidates": boolean,
Expand Down
10 changes: 5 additions & 5 deletions test/output/vscode-smart-clicks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export interface ConfigKeyTypeMap {
"smartClicks.clicksInterval": number,
"smartClicks.triggerDelay": number,
"smartClicks.htmlLanguageIds": string[],
"smartClicks.rules": { 'bracket-pair': boolean; 'dash': boolean; 'html-attr': boolean; 'html-element': boolean; 'html-tag-pair': boolean; 'js-arrow-fn': boolean; 'js-assign': boolean; 'js-block': boolean; 'js-colon': boolean; 'jsx-tag-pair': boolean },
"smartClicks.rules": { 'bracket-pair'?: boolean; 'dash'?: boolean; 'html-attr'?: boolean; 'html-element'?: boolean; 'html-tag-pair'?: boolean; 'js-arrow-fn'?: boolean; 'js-assign'?: boolean; 'js-block'?: boolean; 'js-colon'?: boolean; 'jsx-tag-pair'?: boolean },
}

export interface ConfigShorthandMap {
Expand All @@ -53,7 +53,7 @@ export interface ConfigShorthandTypeMap {
clicksInterval: number,
triggerDelay: number,
htmlLanguageIds: string[],
rules: { 'bracket-pair': boolean; 'dash': boolean; 'html-attr': boolean; 'html-element': boolean; 'html-tag-pair': boolean; 'js-arrow-fn': boolean; 'js-assign': boolean; 'js-block': boolean; 'js-colon': boolean; 'jsx-tag-pair': boolean },
rules: { 'bracket-pair'?: boolean; 'dash'?: boolean; 'html-attr'?: boolean; 'html-element'?: boolean; 'html-tag-pair'?: boolean; 'js-arrow-fn'?: boolean; 'js-assign'?: boolean; 'js-block'?: boolean; 'js-colon'?: boolean; 'jsx-tag-pair'?: boolean },
}

export interface ConfigItem<T extends keyof ConfigKeyTypeMap> {
Expand Down Expand Up @@ -112,7 +112,7 @@ export interface ScopedConfigKeyTypeMap {
"clicksInterval": number,
"triggerDelay": number,
"htmlLanguageIds": string[],
"rules": { 'bracket-pair': boolean; 'dash': boolean; 'html-attr': boolean; 'html-element': boolean; 'html-tag-pair': boolean; 'js-arrow-fn': boolean; 'js-assign': boolean; 'js-block': boolean; 'js-colon': boolean; 'jsx-tag-pair': boolean },
"rules": { 'bracket-pair'?: boolean; 'dash'?: boolean; 'html-attr'?: boolean; 'html-element'?: boolean; 'html-tag-pair'?: boolean; 'js-arrow-fn'?: boolean; 'js-assign'?: boolean; 'js-block'?: boolean; 'js-colon'?: boolean; 'jsx-tag-pair'?: boolean },
}

export const scopedConfigs = {
Expand All @@ -130,14 +130,14 @@ export interface NestedConfigs {
"clicksInterval": number,
"triggerDelay": number,
"htmlLanguageIds": string[],
"rules": { 'bracket-pair': boolean; 'dash': boolean; 'html-attr': boolean; 'html-element': boolean; 'html-tag-pair': boolean; 'js-arrow-fn': boolean; 'js-assign': boolean; 'js-block': boolean; 'js-colon': boolean; 'jsx-tag-pair': boolean },
"rules": { 'bracket-pair'?: boolean; 'dash'?: boolean; 'html-attr'?: boolean; 'html-element'?: boolean; 'html-tag-pair'?: boolean; 'js-arrow-fn'?: boolean; 'js-assign'?: boolean; 'js-block'?: boolean; 'js-colon'?: boolean; 'jsx-tag-pair'?: boolean },
},
}

export interface NestedScopedConfigs {
"clicksInterval": number,
"triggerDelay": number,
"htmlLanguageIds": string[],
"rules": { 'bracket-pair': boolean; 'dash': boolean; 'html-attr': boolean; 'html-element': boolean; 'html-tag-pair': boolean; 'js-arrow-fn': boolean; 'js-assign': boolean; 'js-block': boolean; 'js-colon': boolean; 'jsx-tag-pair': boolean },
"rules": { 'bracket-pair'?: boolean; 'dash'?: boolean; 'html-attr'?: boolean; 'html-element'?: boolean; 'html-tag-pair'?: boolean; 'js-arrow-fn'?: boolean; 'js-assign'?: boolean; 'js-block'?: boolean; 'js-colon'?: boolean; 'jsx-tag-pair'?: boolean },
}

Loading