Query type extractio...

admin

typescript

query type extraction from string const:
другие самплы
type DType = { dia: 1 };
type SomeType = { aa: string }

export type QueryTypes = {
	DType: DType,
    SomeType: SomeType,
}

export const dialogType = `\n    query DType {...`

export type QueryString<T extends keyof QueryTypes> = `\n    ${'query'} ${T} ${string}`

function func<T extends keyof QueryTypes|unknown>(s: T extends keyof QueryTypes ? QueryString<T> : string) : 
T extends keyof QueryTypes ? QueryTypes[T] : T{
    
	let key = s.match(/(query) (\w+)/)?.pop() 
	const r: unknown = key == 'DialogType' ? {dia: 1} : {aa: ''}
    return r as T extends keyof QueryTypes ? QueryTypes[T] : T;
}




let r = func(dialogType)
let r1 = func<{a: boolean}>(dialogType)
(ваш голос учтен)

Прикрепить файл