Skip to content

Instantly share code, notes, and snippets.

@iamshadmirza
Last active April 29, 2021 10:54
Show Gist options
  • Save iamshadmirza/b7adc4e2ffdbd6d8565c58ad3986f5e4 to your computer and use it in GitHub Desktop.
Save iamshadmirza/b7adc4e2ffdbd6d8565c58ad3986f5e4 to your computer and use it in GitHub Desktop.
(function ( ) {
// keyof: This created a literal type where new type will be equal to keyof type
interface Resume {
name: string,
education: string,
skills: Array<string>
}
type ResumeType = keyof Resume;
// keyof will give same result as type KeysOf = 'name' || 'education' || 'skill' (all string literal type)
// typeof gets the type of an object
const resume = {
name: 'Shad',
education: 'Btech',
skills: ['js']
}
type TypeOfResume = typeof resume
// indexed access
type ResumeSkills = Resume['skills'];
// condional types
type isPinnedPassed = true; // checking if the user has passed isPinned = true
type isPinnedOmited = false | null | undefined;
type pinLocation<T extends isPinnedPassed, isPinnedOmited> = T extends isPinnedPassed ? "top" | "bottom" : null;
// I want pinLocation to be required if isPinned is true and not required is isPinned is false
// By not required, I mean user must not pass pinLocation as argument. Should I use `null` or never here
// interface myTest {
// isPinned: boolean
// pinLocation: pinLocation
// }
// how to use this with interface
// mapped types
type resumeType = {
[key in ResumeType]: string
}
})()
// Can we do this with template literal type
type bg = 'red' | 'blue' | 'green'
type shade = 100 | 200 | 300 | 400 | 500
type = backgroundColor = `bg-${bg}-${shade}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment