score:1
for the intellisense part of the question, it looks like the completion list just doesn't include keyof a
when examining keyof b
when b extends a
. it would be nice if it did, but it doesn't. such a change has been suggested. if you want to see this happen you might want to go to that github issue and give it your 👍 or describe your use case if you think it is both compelling and different from what's already there.
for now the workaround could be to change the constraint to be k extends keyof t | keyof myitem
. since keyof t
already must include keyof myitem
, the additional union of keyof myitem
doesn't change anything in terms of what types are valid specifications for k
. but it does bring back the intellisense autocompletion list you expect.
for the "correct type for value
parameter" part of the question, i think i need more elaboration on what you mean. but do be warned: if t extends myitem
, then t["name"]
might be a narrower type than myitem["name"]
. imagine:
interface myitem {
name: string;
}
export function mycomponent<t extends myitem>() {
function handleonchange<k extends keyof t | keyof myitem>(key: k) {
return function (value: t[k]) { }
}
handleonchange("name")("fred"); // wait a minute
}
typescript allows you to call handleonchange("name")(xxx)
with any string
as xxx
, even though this is not safe. observe:
interface myitemnamedbob extends myitem {
name: "bob";
}
mycomponent<myitemnamedbob>(); // ?
a value of type myitemnamedbob
has the literal string "bob"
as the name
at the type level. it cannot be changed to "fred"
. handleonchange("name")
for a generic t
that extends myitem
can't really safely accept anything at all. in practice this might not matter, but you should be careful, since the compiler isn't always.
okay, hope that helps; good luck!
score:0
have you tried doing k keyof t
instead of k extends keyof t
?
Source: stackoverflow.com
Related Query
- Generics that extend a static type aren't intellisensing
- Static type checking with immutable.js and Facebook flow
- Generics error with forwardRef: Property 'ref' does not exist on type 'IntrinsicAttributes'
- styled-components: extend styles and change element type
- static getDerivedStateFromProps that requires previous props and state callback?
- type error extend is not a function in styled components
- TS Expression produces a union type that is too complex to represent with Material-UI and @react-three/fiber
- Typescript: how to declare a type that includes all types extending a common type?
- How to create a typescript type that validates string format? i.e. checks is string is valid css length property
- Missing Properties from Type that shouldn't have to be passed manually to child component
- Typescript extend type if boolean type is true
- Creating custom PropTypes that extend default PropTypes in react
- How to type a Typescript React Component that accepts a Context with a super-set of properties
- What type declaration to use for a generic T that may or may not be hierarchical?
- How to type a React component that accepts another component as a prop, and all of that component's props?
- How to extend NextPage type to add custom field to page component?
- Typescript and React: What is the proper type for an event handler that uses destructuring?
- Type 'undefined' must have a '[Symbol.iterator]()' method that returns an iterator
- How to use generics in an abstract class that is extending a React Component?
- Why typescript does not ensure type safety in a generic higher order component that adds additional properties in React?
- Typescript type declarations for Promise that returns a React Component
- How to typecheck a generic function using a value of that same generic type in Flow
- How to extend type union props to a react class component?
- Flow: How to type a HOC that accepts a prop the wrapped component doesn't care about
- Type HOC that enhances component with its own properties?
- How to add custom property and extend DocumentInitialProps type in _document?
- How to use a lookup type for a static class member?
- TypeScript + React: Enforce that component returns another type of component
- Generate type definitions from JS React components that use `prop-types`
- React typescript - Type must have a '[Symbol.iterator]()' method that returns an iterator
More Query from same tag
- TypeError: app.auth() is not a function? (React App)
- Search a result in next page reactjs
- in a React App, JSON.Stringify shows only first ltem and a length item instead of other items
- What are the benefits of using react-router over kadira:flow-router for meteor
- React - update value in class based on variable outside of class
- How to implement a check all button for radio buttons, using React Hooks?
- React: How to update an object in an array from a child component
- setState unexpectedly updates non-state properties
- How to implement this without triggering an infinite loop with useEffect
- Framework7 - Global State in React Application
- Why is this onClick event handler firing twice in my create-react-app
- How to load child component with props on first render with React Hooks
- React Updater function doesn't work. Regular setState somehow does? (Cannot read property 'name' of null)
- Unexpected behavior when DOM structure is modified and re-rendered in react
- Axios request keeps returning twice undefined and twice the data
- React changing state in Parent , does render calls for all its children and sub-children as iteration?
- How to render two different arrays data into a single array of objects?
- State is not changing in onClick event
- What is the difference between using constructor vs state = {} to declare state in react component?
- Formik FieldArray doesn't accept checkbox Objects
- Using dotenv module with React
- Binding input type using refs in reactjs
- Map in react js
- How do I sort specific questions to a specific section?
- How to convert existing JSON into different JSON Structure
- implementation of react context doesn't work
- Select MenuItem doesn't show when JSX saved to state
- react-burger-menu isOpen prop not reacting to state change
- how to resolve a react native EventEmitterListener warning
- What does ref and node refer to in react-redux?