Код: Выделить всё
type MyType = {
x: number;
y: string;
}
const func = (t : MyType, k : TKey): MyType[TKey] => {
// Do something...
return t[k];
}
const a = func({ x: 1, y: '2' }, 'x') // type: number
const b = func({ x: 1, y: '2' }, 'y') // type: string
Код: Выделить всё
class MyType(TypedDict):
x: int
y: str
class Key:
x: Literal['x'] = 'x'
y: Literal['y'] = 'y'
def func(t: MyType, k: Key) -> ???:
# Do something...
return t[k]
a = func({ Key.x: 1, Key.y: '2' }, Key.x) # (should be) type: int
b = func({ Key.x: 1, Key.y: '2' }, Key.y) # (should be) type: str
Подробнее здесь: https://stackoverflow.com/questions/795 ... e-of-an-ar